#!/usr/bin/perl
use strict;
#use CGI qw( header ); print header();
use Benchmark;
use Devel::OpProf qw(profile zero_stats stats );
profile(1); # turn on profiling
our $now = 8; # we'll pretend it's between 8 and 9 PM
our %url = (
monday => {
@{[map(($_,1), (1..1000))]}
}
);
#start profiling
$|++;
print "
\n";
print "| \n";
my_profile( "grep",\&test_grep );
print " | \n";
my_profile( "if/then/else",\&test_if_then );
print " | \n";
my_profile( "book max()",\&test_max );
print " |
\n";
# now lets benchmark
print "";
timethese(10000, {
bench_grep => q{
test_grep();
},
bench_if_then => q{
test_if_then();
},
bench_max => q{
test_max();
}}
);
print " |
\n";
sub test_grep {
$now = (sort grep {$_ <= $now} keys %{$url{'monday'}})[-1];
}
sub test_if_then {
$now = ($now < $_ && $_ < 8 ? $_ : $now) for keys %{$url{'monday'}};
}
sub test_max {
foreach ( keys %{$url{'monday'}} ) { $now = $_ if $_ > $now }
}
sub my_profile {
my $title = shift;
my $test_sub = shift || return;
zero_stats;
&$test_sub;
stats_td( $title );
# print_stats;
}
sub stats_td {
my $title = shift;
my %stats = %{stats()};
print " $title
|
\n";
while ( my ( $stat, $value ) = each %stats ) {
$stat =~ s/\<\;/;
$stat =~ s/>/\>\;/;
if ( $value ) {
$value =~ s/>/\>\;/;
$value =~ s/\<\;/;
print "| $stat | $value |
\n";
}
}
print "
\n";
}