Hello BrowserUk,

As usual, I'm late to the table, but I've found in my *nix environments that using 'elsif' does improve the performance and adding some minimal binary splitting also improved the performance of your subroutine. I benchmarked both your original and my re-work and results are at the bottom. Obviously if the inputs are better known you could improve the break points. Hope this spurs your imagination a little.

use strict; use warnings; use Benchmark qw(:all); my @input = ( 25000, 50000, 150000, 225000, 300000, 600000, 1200000, 3 +600000, 5400000, 10800000, 21600000, 43200000, 64800000, 1296000 +00, 216000000, 432000000, 864000000, 1728000000, 3024000000, 6048000000, 12096000000, 31557600000, 63115200000, 78894 +000000, 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) @ 4799 +9.55/s (n=107519) case2: 3 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU) @ 6088 +8.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) @ 486 +57.33/s (n=112885) case2: 2 wallclock secs ( 2.24 usr + 0.00 sys = 2.24 CPU) @ 610 +86.61/s (n=136834)

Best Regards...Ed

"Well done is better than well said." - Benjamin Franklin


In reply to Re: A better way of lookup? by flexvault
in thread A better way of lookup? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.