use strict; use warnings; use Benchmark qw(:all); my @input = ( 25000, 50000, 150000, 225000, 300000, 600000, 1200000, 3600000, 5400000, 10800000, 21600000, 43200000, 64800000, 129600000, 216000000, 432000000, 864000000, 1728000000, 3024000000, 6048000000, 12096000000, 31557600000, 63115200000, 78894000000, 157788000000, 315576000000, ); my @newinput = (); for ( 0 .. $#input ) { $newinput[$_] = $input[$_] - 11; # print $newinput[$_],"\n"; # Testing } # print $#newinput,"\n"; # size of input array ## Make sure both subroutines return same results! for my $no ( 0 .. $#newinput ) { my $r1 = &lookup_org ( $newinput[ $no ] ); my $r2 = &lookup_new ( $newinput[ $no ] ); if ( $r1 != $r2 ) { print "$no|$r1|$r2|\n"; exit(1); } # print "$no|$r1|$r2|\n"; # Testing } timethese ( -2 , { case1 => sub { &lookup1 }, case2 => sub { &lookup2 }, }, ); sub lookup1 { for ( 0 .. $#newinput ) { lookup_org ( $newinput[ $_ ] ); } } sub lookup2 { for ( 0 .. $#newinput ) { lookup_new ( $newinput[ $_ ] ); } } sub lookup_org { my $v = shift; if( $v < 25000 ) { return 2500; } if( $v < 50000 ) { return 5000; } if( $v < 150000 ) { return 12500; } if( $v < 225000 ) { return 25000; } if( $v < 300000 ) { return 37500; } if( $v < 600000 ) { return 60000; } if( $v < 1200000 ) { return 120000; } if( $v < 3600000 ) { return 300000; } if( $v < 5400000 ) { return 600000; } if( $v < 10800000 ) { return 900000; } if( $v < 21600000 ) { return 1800000; } if( $v < 43200000 ) { return 3600000; } if( $v < 64800000 ) { return 7200000; } if( $v < 129600000 ) { return 10800000; } if( $v < 216000000 ) { return 21600000; } if( $v < 432000000 ) { return 43200000; } if( $v < 864000000 ) { return 86400000; } if( $v < 1728000000 ) { return 172800000; } if( $v < 3024000000 ) { return 345600000; } if( $v < 6048000000 ) { return 604800000; } if( $v < 12096000000 ) { return 1209600000; } if( $v < 31557600000 ) { return 2629800000; } if( $v < 63115200000 ) { return 5259600000; } if( $v < 78894000000 ) { return 7889400000; } if( $v < 157788000000 ) { return 15778800000; } else { return 31557600000; } } sub lookup_new { my $v = shift; if ( $v >= 64800000 ) { if ( $v < 129600000 ) { return 10800000; } elsif( $v < 216000000 ) { return 21600000; } elsif( $v < 432000000 ) { return 43200000; } elsif( $v < 864000000 ) { return 86400000; } elsif( $v < 1728000000 ) { return 172800000; } elsif( $v < 3024000000 ) { return 345600000; } elsif( $v < 6048000000 ) { return 604800000; } elsif( $v < 12096000000 ) { return 1209600000; } elsif( $v < 31557600000 ) { return 2629800000; } elsif( $v < 63115200000 ) { return 5259600000; } elsif( $v < 78894000000 ) { return 7889400000; } elsif( $v < 157788000000 ) { return 15778800000; } else { return 31557600000; } } else { if ( $v < 25000 ) { return 2500; } elsif( $v < 50000 ) { return 5000; } elsif( $v < 150000 ) { return 12500; } elsif( $v < 225000 ) { return 25000; } elsif( $v < 300000 ) { return 37500; } elsif( $v < 600000 ) { return 60000; } elsif( $v < 1200000 ) { return 120000; } elsif( $v < 3600000 ) { return 300000; } elsif( $v < 5400000 ) { return 600000; } elsif( $v < 10800000 ) { return 900000; } elsif( $v < 21600000 ) { return 1800000; } elsif( $v < 43200000 ) { return 3600000; } elsif( $v < 64800000 ) { return 7200000; } } } __END__ Results: 1st is worst time, 2nd is best time Benchmark: running case1, case2 for at least 2 CPU seconds... case1: 2 wallclock secs ( 2.24 usr + 0.00 sys = 2.24 CPU) @ 47999.55/s (n=107519) case2: 3 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU) @ 60888.83/s (n=125431) Benchmark: running case1, case2 for at least 2 CPU seconds... case1: 3 wallclock secs ( 2.32 usr + 0.00 sys = 2.32 CPU) @ 48657.33/s (n=112885) case2: 2 wallclock secs ( 2.24 usr + 0.00 sys = 2.24 CPU) @ 61086.61/s (n=136834)