True. Of course, in this application, there appear to be a
maximum of 24 possible entries...
Being curious, I did a quick benchmark. However, its
results defy logic (my (sort grep keys)[-1] is
always faster than a O(1) "max()" algorithm!)
Since I am obviously missing something, would you be willing
to post some benchmarking code showing the efficiency
differences between "sort" and "max()" in this context?
Russ
P.S. Just for the sake of helping me find my silliness,
here is what I was running. Changing the map to larger
numbers (to make the source hash bigger) made no difference
to the benchmark times.
#!/usr/bin/perl -w
use strict;
use Benchmark;
my $now = 8; # we'll pretend it's between 8 and 9 PM
my %url = (
monday => {
@{[map(($_,1), (1..1000))]}
}
);
timethese(100000, {
a => q{
$now = (sort grep {$_ <= $now} keys %{$url{'monday'}})[-1];
},
b => q{
$now = ($now < $_ && $_ < 8 ? $_ : $now) for keys %{$url{'monday'}
+};
}
});
The results were the same even without doing the assignment
each time in b (only assign when we should, rather than each
time through the loop). I left it this way for no apparent
reason... |