in reply to Re: How to add more conditional statements in an efficient manner
in thread How to add more conditional statements in an efficient manner

Thanks Huck, If this is ideal for large lookup tables, what method would you recommend for say 5 lockups?

  • Comment on Re^2: How to add more conditional statements in an efficient manner

Replies are listed 'Best First'.
Re^3: How to add more conditional statements in an efficient manner
by huck (Prior) on May 02, 2017 at 21:20 UTC

    It all depends on how many things you are looking up, and how many in the match tree.

    use strict; use warnings; testit(100000,5); testit(100000,50); testit(1000000,5); testit(1000000,50); sub testit { my $inputsize=shift; my $testn=shift; my @list; for my $i(1..$inputsize) { push @list,sprintf('%010d',$i); } my @lookfors; for my $i(1..$testn) { push @lookfors,sprintf('%010d',$i); } my %lookup; for my $lf (@lookfors){ $lookup{$lf}=1; } { my $type='hash'; my $st=time; my $ct=0; for my $test (@list) { unless ($lookup{$test}) {$ct++;} } my $et=time-$st; printf "%5s inputsize:%10d testn:%5d ct:%10d time:%3d \n",$type,$inp +utsize,$testn,$ct,$et; } { # approx an if/the/elsif/else tree my $type='list'; my $st=time; my $ct=0; tests: for my $test (@list) { for my $lf(@lookfors){ if ($test eq $lf) {next tests; } } $ct++; } my $et=time-$st; printf "%5s inputsize:%10d testn:%5d ct:%10d time:%3d \n",$type,$inp +utsize,$testn,$ct,$et; } }
    result
    hash inputsize: 100000 testn: 5 ct: 99995 time: 0 list inputsize: 100000 testn: 5 ct: 99995 time: 0 hash inputsize: 100000 testn: 50 ct: 99950 time: 0 list inputsize: 100000 testn: 50 ct: 99950 time: 1 hash inputsize: 1000000 testn: 5 ct: 999995 time: 0 list inputsize: 1000000 testn: 5 ct: 999995 time: 2 hash inputsize: 1000000 testn: 50 ct: 999950 time: 0 list inputsize: 1000000 testn: 50 ct: 999950 time: 10
    As you an see for a small number of items to test against it doesnt make much of a difference. I would use the hash just because the code is cleaner.

      Thank you Huck, I agree that the hash is cleaner!

Re^3: How to add more conditional statements in an efficient manner
by Anonymous Monk on May 02, 2017 at 19:52 UTC
    'what method would you recommend for say 5 lockups?'

    I would contact my local police authority and ask them.

      making fun of typos made by foreigners is really bad style!

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Lighten up, Francis.

      Man Ya'll are really unforgiving! haha

      Ha ha! Good one. And it's funny because you are NOT making fun of a newbee. Anyone can make typwos. Play on word jokes really ease the growing tensions here at the Monastery.