use Benchmark 'cmpthese'; use List::BinarySearch::XS 'binsearch_pos'; use List::Util 'first'; my @lookup_values = ( [ 25000, 2500 ], [ 50000, 5000 ], [ 150000, 12500 ], [ 225000, 25000 ], [ 300000, 37500 ], [ 600000, 60000 ], [ 1200000, 120000 ], [ 3600000, 300000 ], [ 5400000, 600000 ], [ 10800000, 900000 ], [ 21600000, 1800000 ], [ 43200000, 3600000 ], [ 64800000, 7200000 ], [ 129600000, 10800000 ], [ 216000000, 21600000 ], [ 432000000, 43200000 ], [ 864000000, 86400000 ], [ 1728000000, 172800000 ], [ 3024000000, 345600000 ], [ 6048000000, 604800000 ], [ 12096000000, 1209600000 ], [ 31557600000, 2629800000 ], [ 63115200000, 5259600000 ], [ 78894000000, 7889400000 ], [ 157788000000, 15778800000 ], ); my @tests = ( 0, 100, 25000, 50000, ( map { int rand 160000000000 } 1 .. 10000 ), 160000000000 ); sub simple_lookup { my( $n, $h ) = @_; $n < $_->[0] && return $_->[1] for @$h; return 31557600000; } sub bsearch_lookup { my( $n, $h ) = @_; my $pos = binsearch_pos { $a <=> $b->[0] } $n+1, @$h; $pos > $#$h && return 31557600000; return $h->[$pos][1]; } sub first_lookup { my( $n, $h ) = @_; my $res = first { $n < $_->[0] } @$h; return ref $res ? $res->[1] : 31557600000; } cmpthese( -5, { simple => sub { my @result = map{ bsearch_lookup( $_, \@lookup_values ) } @tests; }, bsearch => sub { my @result = map{ simple_lookup ( $_, \@lookup_values ) } @tests; }, first => sub { my @result = map{ first_lookup ( $_, \@lookup_values ) } @tests; }, });