in reply to Re: "exists $hash{key}" is slower than "$hash{key}"
in thread "exists $hash{key}" is slower than "$hash{key}"
If you want to benchmark code using lexical variables, but need to ameliorate the overhead of calling a sub, usually the easiest way is to wrap the code you're benchmarking in a loop that gets executed thousands of times...
#!/usr/bin/perl use warnings; use strict; use Benchmark qw{ cmpthese }; my %h; @h{1001 .. 2000} = (1) x 1000; my ($e, $v) = (0) x 2; cmpthese(-1, { exist => sub { for (0..999_999) { ++$e if exists $h{1001} } }, value => sub { for (0..999_999) { ++$v if $h{1001} } }, }); __DATA__ Rate value exist value 18.3/s -- -5% exist 19.3/s 5% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: "exists $hash{key}" is slower than "$hash{key}"
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 swl (Prior) on Jan 08, 2020 at 23:52 UTC | |
by LanX (Saint) on Jan 07, 2020 at 13:37 UTC | |
by swl (Prior) on Jan 08, 2020 at 23:50 UTC |