#!/usr/bin/perl -w my $nkeys = 10; my @keys; our $key; # (keep last key for lookup below) for (1..$nkeys) { # generate random keys consisting of 3..10 chars $key = join "", map chr(97+int(rand(26))), (1..3+int(rand(8))); push @keys, $key; } # setup 'if'-code my $code = "sub isMember_if {\n" . join("", map "return 1 if \$_[0] eq '$_';\n", @keys) . "return 0;}"; #print "$code\n"; eval $code; # setup hash my %hash = map { $_ => 1 } @keys; sub isMember_hash { return $hash{$_[0]} }; use Benchmark 'cmpthese'; cmpthese( -1, { 'if' => 'isMember_if($key)', 'hash' => 'isMember_hash($key)' }); #### Rate if hash if 628278/s -- -68% hash 1946613/s 210% --