in reply to "exists $hash{key}" is slower than "$hash{key}"
5.26.1 on Linux:#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Benchmark qw{ cmpthese }; my %hl; @hl{1001 .. 2000} = (1) x 1000; our %hg = %hl; our ($el, $vl, $eg, $vg) = (0) x 4; die unless $hl{1001}; die unless $hg{1001}; cmpthese(-1, { exist_l => q{ ++$el if exists $hl{1001} }, value_l => q{ ++$vl if $hl{1001} }, exist_g => q{ ++$eg if exists $hg{1001} }, value_g => q{ ++$vg if $hg{1001} }, }); say join "\n", "el: $el", "vl: $vl", "eg: $eg", "vg: $vg";
Blead gives smaller differences, but the order is the same.Rate value_g exist_g value_l exist_l value_g 17625636/s -- -4% -47% -53% exist_g 18303545/s 4% -- -45% -51% value_l 33363781/s 89% 82% -- -11% exist_l 37417325/s 112% 104% 12% -- el: 0 vl: 0 eg: 23229990 vg: 23229990
Update: Changing q{ to sub { changes the order randomly and makes the difference less than 10%.
Update 2: Changing 1001 to 2001 in the benchmarked code (i.e. testing non-existent key) changes the differences to 20% and less, e.g.
Rate value_g exist_g value_l exist_l value_g 31977080/s -- -2% -8% -19% exist_g 32733979/s 2% -- -6% -17% value_l 34737228/s 9% 6% -- -12% exist_l 39658833/s 24% 21% 14% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: "exists $hash{key}" is slower than "$hash{key}"
by tobyink (Canon) on Jan 06, 2020 at 21:41 UTC | |
by swl (Prior) on Jan 06, 2020 at 23:56 UTC | |
by LanX (Saint) on Jan 07, 2020 at 02:30 UTC | |
by choroba (Cardinal) on Jan 07, 2020 at 09:53 UTC | |
by swl (Prior) on Jan 07, 2020 at 03:12 UTC | |
by tobyink (Canon) on Jan 07, 2020 at 08:30 UTC | |
| |
by LanX (Saint) on Jan 07, 2020 at 13:37 UTC | |
| |
|
Re^2: "exists $hash{key}" is slower than "$hash{key}"
by swl (Prior) on Jan 07, 2020 at 00:10 UTC |