http://www.perlmonks.org?node_id=11114845


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

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

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

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

        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