Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: declaring lexical variables in shortest scope: performance?

by haukex (Chancellor)
on Mar 31, 2020 at 10:38 UTC ( #11114841=note: print w/replies, xml ) Need Help??


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

At least on my 5.28, the difference is negligible:

use warnings; use strict; use Benchmark 'cmpthese'; cmpthese(-2, { predecl => sub { my $y; my $x; for $x (1,2,3) { $y+=$x } }, lexical => sub { my $y; for my $x (1,2,3) { $y+=$x } }, }); __END__ Rate predecl lexical predecl 9175035/s -- -1% lexical 9275893/s 1% --
But wakes in me primordial fears

Yes, I know the feeling well. But my philosophy has become: first, code so that it works, avoiding only the really obvious performance mistakes (like scanning an array instead of using a hash and the like). Then, if it's fast enough for your puproses, you're done. But if you want to optimize, remember that optimization is a science: measure the performance, identify the hotspots, benchmark the alternatives, modify the code accordingly, measure the difference in performance, and repeat until the performance becomes good enough for your purposes.

Replies are listed 'Best First'.
Re^2: declaring lexical variables in shortest scope: performance?
by bliako (Parson) on Mar 31, 2020 at 11:05 UTC

    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

      > 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% --
Re^2: declaring lexical variables in shortest scope: performance?
by Anonymous Monk on Mar 31, 2020 at 11:52 UTC
    Maybe it is negligible but that is Benchmark pitfall, overhead drowns out what you're measuring, in line sub contents as strings not sub calls
      overhead drowns out what you're measuring, in line sub contents as strings not sub calls

      Can you show the code that demonstrates this?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2020-06-01 16:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (4 votes). Check out past polls.

    Notices?