Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by bliako (Monsignor)
on Mar 31, 2020 at 11:58 UTC ( [id://11114850]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-25 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found