Next | Making Programs Faster | 116 |
Postscript: In 2005 I gave this class at OSCON
An audience member interrupted to say he had found an obvious way to speed up letter_histogram
He had benchmarked it and found it substantially faster
His benchmark looked something like this:
use Benchmark; my $t = "some reasonably long string here"; timethese(-5, { orig => 'orig_letter_histogram($t)' , mine => 'my_letter_histogram($t)' , });
sub orig_letter_histogram { my $strdex = (length $_[0])-1; $letter_hist{substr($_[0],$_,1)}++ for (0..$strdex); } sub my_letter_histogram { $letter_hist{$1}++ while $_[0] =~ m/(.)//gs; }
The following week, I did it right
His suggestion is 250% slower:
orig histo 11.42 0.03 11.45 while //gs 37.28 0.03 37.31 NULL 0.04 0.00 0.04
Next | Copyright © 2003 M. J. Dominus |