Okay, here is the max() given in Mastering Algorithms with Perl (O'Reilly):
sub max { # Numbers.
my $max = shift;
foreach ( @_ ) { $max = $_ if $_ > $max }
return $max;
}
So, modifying that to fit into our existing benchmark/profiling code, it looks like:
foreach ( keys %{$url{'monday'}} ) { $now = $_ if $_ > $now }
And then, the score:
grep
|
| constant item | 2 |
| numeric le (<=) | 1000 |
| shift | 1 |
| sort | 1 |
| hash dereference | 2 |
| hash element | 1 |
| pushmark | 10 |
| grep | 1 |
| grep iterator | 1000 |
| scalar variable | 2001 |
| next statement | 6 |
| list slice | 1 |
| scalar assignment | 2 |
| keys | 1 |
| subroutine exit | 1 |
| block | 1000 |
| array dereference | 1 |
| glob value | 5 |
| private variable | 3 |
| subroutine entry | 4 |
|
if/then/else
|
| constant item | 993 |
| foreach loop entry | 1 |
| shift | 1 |
| foreach loop iterator | 1001 |
| iteration finalizer | 1000 |
| hash dereference | 2 |
| hash element | 1 |
| pushmark | 7 |
| scalar variable | 4992 |
| next statement | 1007 |
| logical and (&&) | 2001 |
| scalar assignment | 1001 |
| conditional expression | 1000 |
| keys | 1 |
| subroutine exit | 1 |
| loop exit | 1 |
| array dereference | 1 |
| glob value | 6 |
| private variable | 3 |
| subroutine entry | 4 |
| numeric lt (<) | 1992 |
|
book max()
|
| constant item | 1 |
| foreach loop entry | 1 |
| shift | 1 |
| foreach loop iterator | 1001 |
| iteration finalizer | 1000 |
| hash dereference | 2 |
| numeric gt (>) | 1000 |
| hash element | 1 |
| pushmark | 7 |
| scalar variable | 2292 |
| next statement | 2006 |
| logical and (&&) | 2001 |
| scalar assignment | 147 |
| keys | 1 |
| subroutine exit | 1 |
| loop exit | 1 |
| array dereference | 1 |
| glob value | 6 |
| private variable | 3 |
| subroutine entry | 4 |
|
Benchmark: timing 10000 iterations of bench_grep, bench_if_then, bench_max...
bench_grep: 115 wallclock secs (111.04 usr + 0.01 sys = 111.05 CPU) @ 90.05/s (n=10000)
bench_if_then: 73 wallclock secs (70.02 usr + 0.01 sys = 70.03 CPU) @ 142.80/s (n=10000)
bench_max: 64 wallclock secs (62.72 usr + 0.01 sys = 62.73 CPU) @ 159.41/s (n=10000)
|
Well, we have some surprises here! grep comes in last, even though it has the least operations. As you can see, not all operations are equal... but the book technique is the big winner. Which tells me it was worth the $35US.
The results also show that Devel::OpProf is a useful tool, but only as an accessory to Benchmark. On it's own, it can be misleading.
Paris Sinclair | 4a75737420416e6f74686572
pariss@efn.org | 205065726c204861636b6572
I wear my Geek Code on my finger.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.