Skip to content

Instantly share code, notes, and snippets.

@toke
Created December 8, 2011 11:18
Show Gist options
  • Save toke/1446745 to your computer and use it in GitHub Desktop.
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.
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;
}
@toke
Copy link
Author

toke commented Dec 8, 2011

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment