Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by LanX (Cardinal)
on Mar 31, 2020 at 12:31 UTC ( #11114856=note: print w/replies, xml ) Need Help??


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

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

  • 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 (Parson) 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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2020-07-08 13:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?