On Sat, Dec 04, 2004 at 06:31:35AM +0300, Alexey Trofimenko wrote:
: 
:   for 1..10_000_000 {
:      my ($a,$b,$c) = ...
:      ...
:   }
: 
: vs.
: 
:   for 1..10_000_000 {
:      state ($a,$b,$c) = ...
:      ...
:   }
: 
: latter looks like it would run faster, because no reallocation envolved  
: here.

No reassignment either, since assignment to a state declaration only
happens at "first" time.  If you expect $a, $b, and $c to get new values
each time through the loop, you'll be disappointed.

: I've read an advice somewhat like that in Ruby docs, tried it on perl5,  
: and it really makes a difference, especially on very short loops.
: could it be done some tricky optimisation, so if some variable in loop  
: isn't going to have references on it, stored somewhere outside the block,  
: than C<my> before it will be changed to C<state>?

Er, how did you try a state variable in Perl 5?  It doesn't have state
variables...

Perl 5 already stores all the lexicals in the pad for the entire
subroutine.  There is no separate pad for the inside of the loop.
Any differences in performance would be related to the actual
reinitialization of the lexical, not allocation.

So optimizing to a state variable won't necessarily help your loop
overhead, but it could help your subroutine overhead, at least in Perl
5, if Perl 5 had state variables.  Best you can do in Perl 5 is an
"our" variable with an obscure name.

Larry

Reply via email to