$ perl -we 'print log(78894000000/63115200000)' 0.22314355131421
Using a suitably small log base, one is assured that no more than one value step can occur between log()..log()+1. Here, int(4.5*log(...)) will do nicely.
#! /usr/bin/perl -l use List::Util qw(first min max); use Data::Dumper; my @G = ( [ 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 ], ); # construct the lookup table. my @TT = map { my $i=$_; first { $i <= tti($_->[0]) } @G } 0..tti(~0); push @TT, [~0, 31557600000]; print Dumper \@TT; sub tti { -45 + int (4.5 * log(min(max(25000,shift),157788000000))) } sub v2r { my ($v, $i) = ($_[0], tti($_[0])); $TT[$i + ($v >= $TT[$i][0])][1]; } for (map { $_-1, $_, int 1.2*$_ } map $_->[0], @G) { print "v2r($_) == @{[v2r($_)]}"; }
Split array and combined array variants ought be benched as well. In practice, computing the log might be more cumbersome than a small search.
In reply to Re: A better way of lookup?
by Anonymous Monk
in thread A better way of lookup?
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |