Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by huck (Prior)
on May 02, 2017 at 18:46 UTC ( [id://1189340]=note: print w/replies, xml ) Need Help??


in reply to How to add more conditional statements in an efficient manner

There are a number of ways to do this, and a number of reasons to do it one way or another

Using a lookup hash can be conceptually easy to understand but slower to run for small numbers of lookups.

my %lookup; $lookup{"00000490"}=1; $lookup{"00000290"}=1; ... unless ($lookup{$sUnpackedCDPcap}) { ##Do some stuff## }
For large sparse lookup tables this will be faster than a linear search tho

Replies are listed 'Best First'.
Re^2: How to add more conditional statements in an efficient manner
by adamZ88 (Beadle) on May 02, 2017 at 19:05 UTC

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

      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!

      '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!

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1189340]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-18 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found