my %done;
my @data = (1..100);
for (1..100) {
push @data, int (rand() * 100);
}
for my $item (@data) {
next if exists $done{$item};
# do something time consuming
# ...
$done{$item}++;
}
####
use Benchmark qw {:all};
use 5.016;
my %hash;
# set up the hash
for (1001..2000) {
$hash{$_}++;
}
# two keys we use below
our $key1 = 1001;
our $key2 = 1002;
# hash key 1 is SV
$hash{$key1} = 1;
# hash key 2 is RV
$hash{$key2} = {1..10};
# assign to global as a baseline
our $xx_global;
# keys are short so the timing table is not too wide
# char 1: e = exists check,
# v = value check
# chars 2,3: ck = constant key,
# vk = variable key
# chars 4,5: sv = key contains scalar value,
# rv = key contains reference
# char 6: l = assign to lexical,
# g = assign to global
# thus
# ecksvl means "exists check
# using constant key
# containing a scalar value,
# assigned to lexical"
# (the value is clearly redundant for an exists check,
# but is retained for completeness)
my %checks = (
ecksvl => 'my $x = exists $hash{1001}',
evksvl => 'my $x = exists $hash{$key1}',
vcksvl => 'my $x = $hash{1001}',
vvksvl => 'my $x = $hash{$key1}',
evksvg => '$xx_global = exists $hash{$key1}',
vvksvg => '$xx_global = $hash{$key1}',
eckrvl => 'my $x = exists $hash{1002}',
evkrvl => 'my $x = exists $hash{$key2}',
vckrvl => 'my $x = $hash{1002}',
vvkrvl => 'my $x = $hash{$key2}',
evkrvg => '$xx_global = exists $hash{$key2}',
vvkrvg => '$xx_global = $hash{$key2}',
);
cmpthese (
-3,
\%checks
);
####
Rate evksvl evkrvl ecksvl eckrvl evksvg evkrvg vvksvl vvkrvl vckrvl vcksvl vvksvg vvkrvg
evksvl 10733145/s -- -5% -7% -12% -15% -18% -29% -31% -32% -33% -41% -48%
evkrvl 11290643/s 5% -- -2% -7% -10% -14% -25% -27% -28% -29% -38% -45%
ecksvl 11570664/s 8% 2% -- -5% -8% -12% -23% -25% -27% -27% -36% -44%
eckrvl 12176232/s 13% 8% 5% -- -3% -8% -19% -21% -23% -23% -33% -41%
evksvg 12572221/s 17% 11% 9% 3% -- -5% -17% -19% -20% -21% -31% -39%
evkrvg 13168623/s 23% 17% 14% 8% 5% -- -13% -15% -17% -17% -28% -36%
vvksvl 15082826/s 41% 34% 30% 24% 20% 15% -- -2% -4% -5% -17% -27%
vvkrvl 15461840/s 44% 37% 34% 27% 23% 17% 3% -- -2% -3% -15% -25%
vckrvl 15777625/s 47% 40% 36% 30% 25% 20% 5% 2% -- -1% -13% -23%
vcksvl 15909705/s 48% 41% 38% 31% 27% 21% 5% 3% 1% -- -13% -23%
vvksvg 18207860/s 70% 61% 57% 50% 45% 38% 21% 18% 15% 14% -- -12%
vvkrvg 20580512/s 92% 82% 78% 69% 64% 56% 36% 33% 30% 29% 13% --