larryl has asked for the wisdom of the Perl Monks concerning the following question:
I tried a few benchmarks in response to discussion around
Big Willy's Frequency Analyzer. The code I tested
and the results can be summarized by:
- chop: 909.92/s
while ($_ ne '') { $letters{lc chop}++; } - split_0: 519.41/s
$letters{$_}++ for split//, lc; - split_1: 481.23/s
$letters{lc $_}++ for split//; - substr: 475.51/s
while ($_ ne '') { $letters{lc substr($_,-1)}++; substr($_, -1) = ""; }
I have two questions:
- I'm not that familiar with the for
syntax used in the split_[01] tests.
Can someone help me mentally parse those, and possibly
explain why one would be faster than the other?
- It seems odd that chop() benchmarks so much faster than substr($_,-1). I've seen the same in other stuff I've benchmarked. Wouldn't you think the compiler would treat substr($_,-1) as a special case? (Ok, that was really just one question and one musing...)
Back to
Seekers of Perl Wisdom