in reply to Re^2: Count byte/character occurrence (1/4)
in thread Count byte/character occurrence (quickly)
would something with a for loop and substr be faster? I dont necessarily have to unpack any bytes, do I?
That requires a call into C (substr) for every byte; where using unpack requires a single call for the entire string.
The cardinal rule for optimising Perl code, is to get perl's built-ins to do as much of the work as you can.
Using this loop:
in place of the unpack loop is almost but not quite as fast.++$seen[ ord chop $buf ] while length $buf;
It trades 2 built-in calls per byte, against the cost of building the unpack return list on the stack, and loses by a hair.
|
---|