Created
December 8, 2011 11:18
-
-
Save toke/1446745 to your computer and use it in GitHub Desktop.
Check if small hashmap is faster than if-cascade. This test is just for fun not for real Performance reasons.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Benchmark qw(:all); | |
use Benchmark ':hireswallclock'; | |
my %special_tariffs = (1166 => 1, 1390 => 1, | |
1229 => 1, | |
1412 => 1, 1413 => 1 | |
); | |
my $b; | |
my $count = 10000000; | |
$b = timethese($count, {t1 => \&fun1}); | |
$b = timethese($count, {t2 => \&fun2}); | |
sub fun1 { | |
my $tarif_id = shift || 11111; | |
if (exists $special_tariffs{$tarif_id}){ | |
$rc = $special_tariffs{$tarif_id}; | |
} | |
return $rc; | |
} | |
sub fun2 { | |
my $tarif_id = shift || 11111; | |
if (($tarif_id == 1166) || ($tarif_id == 1390)) { | |
$rc=1; | |
} elsif ($tarif_id == 1229) { | |
$rc=1; | |
} elsif (($tarif_id == 1412) || ($tarif_id == 1413)) { | |
$rc=1; | |
} | |
return $rc; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Benchmark: timing 10000000 iterations of t1...
t1: 2.00058 wallclock secs ( 2.00 usr + 0.00 sys = 2.00 CPU) @ 5000000.00/s (n=10000000)
Benchmark: timing 10000000 iterations of t2...
t2: 3.02559 wallclock secs ( 3.03 usr + 0.00 sys = 3.03 CPU) @ 3300330.03/s (n=10000000)