Next | Making Programs Faster | 161 |
Back to the MFA
The profiler says that main::letter_histogram is consuming most of the CPU time
%Time ExclSec CumulS #Calls sec/call Csec/c Name
63.7 0.830 0.829 109 0.0076 0.0076 main::letter_histogram
13.8 0.180 0.180 1 0.1800 0.1800 Mail::Util::read_mbox
A 20% speedup in this one function would reduce the program's run time by 1/8
sub letter_histogram { my $strdex = (length $_[0])-1; $letter_hist{substr($_[0],$_,1)}++ for (0..$strdex); }
Perhaps loop over the characters directly
Instead of looping over 0 .. $strdex and indexing the string?
sub letter_histogram { $letter_hist{$_}++ for split //, $_[0]; }
Before After
real 0m2.739s real 0m5.379s user 0m1.270s user 0m2.410s sys 0m0.040s sys 0m0.040s
Well, that didn't work
Next | Copyright © 2003 M. J. Dominus |