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 );