I think it is the because sort() is very fast, and you are comparing it with an if/then (?:) control structure, which is inherantly slow.
Well, another thing to do besides benchmarking is profiling:
#!/usr/bin/perl
use strict;
use Devel::OpProf qw(profile print_stats zero_stats );
profile(1); # turn on profiling
my $now = 8; # we'll pretend it's between 8 and 9 PM
my %url = (
monday => {
@{[map(($_,1), (1..1000))]}
}
);
#start profiling
zero_stats;
$now = (sort grep {$_ <= $now} keys %{$url{'monday'}})[-1];
print_stats;
zero_stats;
$now = ($now < $_ && $_ < 8 ? $_ : $now) for keys %{$url{'monday'}};
print_stats;
Well, this tells us a lot!
| First algorithm |
| null operation | 1006 |
| block | 1001 |
| private variable | 1001 |
| grep iterator | 1000 |
| numeric le (<=) | 1000 |
| scalar variable | 1000 |
| pushmark | 7 |
| next statement | 5 |
| glob value | 3 |
| constant item | 2 |
| subroutine entry | 2 |
| private hash | 1 |
| private array | 1 |
| sort | 1 |
| keys | 1 |
| conditional expression | 1 |
| list slice | 1 |
| hash dereference | 1 |
| hash element | 1 |
| array dereference | 1 |
| scalar assignment | 1 |
| block entry | 1 |
| grep | 1 |
|
| Second algorithm |
| private variable | 3000 |
| null operation | 2006 |
| logical and (&&) | 2001 |
| glob value | 1996 |
| numeric lt (<) | 1992 |
| scalar dereference | 1992 |
| next statement | 1006 |
| conditional expression | 1001 |
| foreach loop iterator | 1001 |
| scalar assignment | 1000 |
| iteration finalizer | 1000 |
| constant item | 993 |
| pushmark | 4 |
| subroutine entry | 2 |
| block | 1 |
| keys | 1 |
| foreach loop entry | 1 |
| array dereference | 1 |
| block entry | 1 |
| private array | 1 |
| private hash | 1 |
| hash dereference | 1 |
| hash element | 1 |
| loop exit | 1 |
|
So we see that just the control loop by iself takes almost as much "action" as the whole first algorithm
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.