Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: "exists $hash{key}" is slower than "$hash{key}"

by swl (Parson)
on Jan 06, 2020 at 23:56 UTC ( [id://11111089]=note: print w/replies, xml ) Need Help??


in reply to Re^2: "exists $hash{key}" is slower than "$hash{key}"
in thread "exists $hash{key}" is slower than "$hash{key}"

Thanks for this. The interesting thing is that if I stringify the code to avoid the sub call then I can replicate the ordering from my original post.

I added a false value to get a sense of the cost of the increment operation. value_true is value from your version, and is obviously faster as it does less work.

use warnings; use strict; use Benchmark qw{ cmpthese }; my %h; @h{1001 .. 2000} = (1) x 1000; my ($e, $vt, $vf) = (0) x 3; $h{1002} = 0; cmpthese(-2, { exist => sub { for (0..999_999) { ++$e if exists $h{1001} } }, exist_str => 'for (0..999_999) { ++$e if exists $h{1001} }', value_true => sub { for (0..999_999) { ++$vt if $h{1001} } }, value_str => 'for (0..999_999) { ++$vt if $h{1001} }', value_false => sub { for (0..999_999) { ++$vf if $h{1002} } }, }); __DATA__ Rate exist value_true exist_str value_str val +ue_false exist 16.1/s -- -9% -23% -26% + -27% value_true 17.7/s 10% -- -15% -18% + -20% exist_str 20.9/s 29% 18% -- -4% + -6% value_str 21.7/s 34% 22% 4% -- + -2% value_false 22.1/s 37% 25% 6% 2% + --

Replies are listed 'Best First'.
Re^4: "exists $hash{key}" is slower than "$hash{key}"
by LanX (Saint) on Jan 07, 2020 at 02:30 UTC
    > if I stringify the code to avoid the sub call then I can replicate the ordering from my original post.

    I'm not sure if you got the point, that accessing an outer lexical variable inside a string is not possible.

    Did you?

    The Benchmark module is eventually running the code's text thru eval , but inside it's own scope.

    The my ($e, $vt, $vf) = (0) x 3; are not accessible from there.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      > The Benchmark module is eventually running the code's text thru eval , but inside it's own scope.

      Not only that, it also turns off strict, so Perl doesn't tell you the hash you're trying to access doesn't exist.

      sub _doeval { no strict; eval shift }

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      The point of the exercise is not to access lexical variables. They are used only so something happens in case that code will otherwise be optimised away (I don't know the internals well enough to be sure). I should have been clearer about this in my original post.

        But %h is a lexical variable too, and the point of the exercise is to access that.

        > The point of the exercise is not to access lexical variables.

        But you are measuring them too.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11111089]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-19 07:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found