Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

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


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

Thanks for the compare script. Indeed the lexical is a bit faster (probably what tobyink said about internal optimisations) and is confirmed by choroba but when I declare a lexical variable inside the loop for my $x (1,2,3) {my $z=12; $y+=$x } in predecl, it's 50% slower :(

I keep what you said about optimisation is a science

Replies are listed 'Best First'.
Re^3: declaring lexical variables in shortest scope: performance?
by choroba (Cardinal) on Mar 31, 2020 at 11:27 UTC
    > it's 50% slower

    Do you mean you added the my $z=12; to both the subroutines and the lexical one became 50% slower? I can't reproduce that behaviour. Adding it to only one of the subs slows it (35% in my case), but then we are comparing apples and oranges.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      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% --

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found