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

> 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

Replies are listed 'Best First'.
Re^5: "exists $hash{key}" is slower than "$hash{key}"
by choroba (Cardinal) on Jan 07, 2020 at 09:53 UTC
    > 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]
Re^5: "exists $hash{key}" is slower than "$hash{key}"
by swl (Prior) on Jan 07, 2020 at 03:12 UTC

    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 was to assess the relative difference exists $h{$k} vs $h{$k}, not the absolute speed. A few more details are in 11111212.

        Apologies for the confusion and/or lack of clarity.

      > 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

        Yes, the lexical variables are part of the absolute timings.

        However, it's the relative timings that are of interest in this case as the point of the exercise was to assess the difference between exists $h{$k} or $h{$k}. If all other components are held constant then the timing differences should be due to the use of either of the two idioms (noting that the benchmarks need to be updated given dave_the_m's comments in 341121).