in reply to Re^3: declaring lexical variables in shortest scope: performance?
in thread declaring lexical variables in shortest scope: performance?

I meant to say that comparing lexicals only: one with a my $z; inside the loop is so much slower. Even 35% is much

here is the code:

use Benchmark 'cmpthese'; cmpthese(-2, { predecl => ' my $y; my $x; for $x (1..10000) { $y+=$x } #print "$y\n"; ', lexical => ' my $y; for my $x (1..10000) { $y+=$x } #print "$y\n"; ', }); __END__
Rate predecl lexical predecl 1836/s -- -54% lexical 3996/s 118% --

Replies are listed 'Best First'.
Re^5: declaring lexical variables in shortest scope: performance?
by LanX (Saint) on Mar 31, 2020 at 12:00 UTC

      what do you mean by aliasing? if you mean accessing complex structures $x->{'a'}->{'b'}->[$loop_index] , I always put the static part of the data structure outside the loop and use that "alias?" inside the loop

        perlglossary#alias

        • alias

          A nickname for something, which behaves in all ways as though you’d used the original name instead of the nickname. Temporary aliases are implicitly created in the loop variable for foreach loops, in the $_ variable for map or grep operators, in $a and $b during sort’s comparison function, and in each element of @_ for the actual arguments of a subroutine call. Permanent aliases are explicitly created in packages by importing symbols or by assignment to typeglobs. Lexically scoped aliases for package variables are explicitly created by the our declaration.

        just search and you'll find many discussions in the archive regarding the complicated mechanisms involved here, including the divide between package and private vars, closure inside the loop, etc ...

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery