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

perlglossary#alias

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

  • Comment on Re^7: declaring lexical variables in shortest scope: performance?

Replies are listed 'Best First'.
Re^8: declaring lexical variables in shortest scope: performance?
by bliako (Abbot) on Mar 31, 2020 at 12:41 UTC

    got it, thanks.

    use Benchmark 'cmpthese'; cmpthese(-2, { predecl => ' my $y; my $x; for $x (1..10000) { $y+=$x } ', lexical => ' my $y; for my $x (1..10000) { $y+=$x } ', aliased => ' my $y; for (1..10000) { $y+=$_ } ', });

    Seems you are right!

    Rate aliased lexical predecl aliased 3733/s -- -3% -5% lexical 3858/s 3% -- -2% predecl 3938/s 5% 2% --

      FWIW, I get what you get now and then and this or between just about as often–

      Rate predecl aliased lexical predecl 3106/s -- -0% -0% aliased 3121/s 0% -- 0% lexical 3121/s 0% 0% -- # This is perl 5, version 18, subversion 4 (v5.18.4) # built for darwin-thread-multi-2level