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.
|