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

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.

Replies are listed 'Best First'.
Re^4: How to add more conditional statements in an efficient manner
by adamZ88 (Beadle) on May 07, 2017 at 04:14 UTC

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