The Perl You Need to Know: Benchmarking Perl Page 4
wordOrd_a
sub wordOrd_a {
my ()=@_;
my @ordStr=();
foreach my (split //,) {
push (@ordStr,ord());
}
return join(" ",@ordStr);
}
The second candidate, wordOrd_b, is nearly identical to the first candidate, but substitutes the
Perl map function for the foreach loop that we relied on in the first attempt.
wordOrd_b
sub wordOrd_b {
my ()=@_;
my @ordStr=();
@ordStr=map { ord sh } split //,;
return join (" ",@ordStr);
}
The third candidate is super-compact, doing away with the split entirely and instead using
Perl's unpack method in a technique gleaned gleefully from the ord synopsis on page 114 of
Perl in a Nutshell (O'Reilly & Associates, 1998). We also dropped the interim variable
and instead refer to the first item in the incoming parameter list directly.
wordOrd_c
sub wordOrd_c {
return join (" ",unpack('C*',sh[0]));
}
Now, for the race. With all three subroutines revved up and raring to go, we can call upon
Benchmark's cmpthese function to test and compare the execution times of the three
candidates, resulting in a revealing summary table.
use Benchmark;
my =500000;
my ="green apples and yellow oranges, oh my";
&Benchmark::cmpthese(,
{
wordOrd_a => q/my =&wordOrd_a()/,
wordOrd_b => q/my =&wordOrd_b()/,
wordOrd_c => q/my =&wordOrd_c()/,
}
);
The computer begins to whir, steam, and belch, and in an exhausted heap dutifully reports its
tally:
Benchmark: timing 500000 iterations of wordOrd_a,
wordOrd_b, wordOrd_c...
wordOrd_a: 4 wallclock secs ( 3.55 usr +
0.00 sys = 3.55 CPU) @ 141003.95/s
(n=500000)
wordOrd_b: 2 wallclock secs ( 2.54 usr +
0.00 sys = 2.54 CPU) @ 196540.88/s
(n=500000)
wordOrd_c: 2 wallclock secs ( 1.64 usr +
0.00 sys = 1.64 CPU) @ 304506.70/s
(n=500000)
Rate wordOrd_a wordOrd_b wordOrd_c
wordOrd_a 141004/s -- -28% -54%
wordOrd_b 196541/s 39% -- -35%
wordOrd_c 304507/s 116% 55% --
The winner, not surprisingly, is the code that appeared most efficient: version c, the unpack
solution. Indeed, from the numbers we can see that wordOrd_c ran 304,506.70 times per
second, quite a bit faster than the closest competitor in wordOrd_b. The nifty table provided by
cmpthese displays the results in startling percentages: wordOrd_c was 116% faster than
wordOrd_a and 55% faster than wordOrd_b.
0 Comments (click to add your comment)
Networking Solutions
