use List::BinarySearch::XS 'binsearch_pos'; 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 ], ); sub bsearch_lookup { my( $n, $h ) = @_; my $pos = binsearch_pos { $a+1 <=> $b->[0] } $n, @$h; $pos > $#$h && return 31557600000; return $h->[$pos][1]; } my @tests = ( 100, 25000, 50000, ( map { int rand 160000000000 } 1 .. 10 ), 160000000000 ); # Make sure we have some edge cases foreach my $test ( @tests ) { my $bmatch = bsearch_lookup( $test, \@lookup_values ); print "Bsearch Found : $test => $bmatch\n\n";