nemesisgus has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
After reading this old but interesting thread: About "state" variables in Perl 5.10, it came to my mind if there is any difference in performance between this:
my $n; for (1..10000000) { $n = $_**2; }
and this:
for (1..10000000) { state $n = $_**2; }
given that, afaik, if we rewrite the second example using 'my', it does have an extra cost at runtime, since it is run once each time execution passes the 'my' declaration:
for (1..10000000) { my $n = $_**2; }
Is this "extra" time cost also present in the case of declaring the variable with 'state' although the variable is not reinitialized each time?
Thanks and sorry for the silly question.
PS: I know the second example generates a different output, but I'm mostly interested in the performance effect.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: 'my' and 'state' declaration inside a loop
by davido (Cardinal) on Aug 05, 2012 at 02:38 UTC | |
|
Re: 'my' and 'state' declaration inside a loop
by 2teez (Vicar) on Aug 05, 2012 at 03:08 UTC | |
by nemesisgus (Acolyte) on Aug 05, 2012 at 16:22 UTC | |
by GrandFather (Saint) on Aug 07, 2012 at 20:32 UTC | |
|
Re: 'my' and 'state' declaration inside a loop
by Marshall (Canon) on Aug 05, 2012 at 17:14 UTC | |
by nemesisgus (Acolyte) on Aug 05, 2012 at 19:31 UTC | |
by Marshall (Canon) on Aug 08, 2012 at 04:06 UTC | |
|
Re: 'my' and 'state' declaration inside a loop
by Anonymous Monk on Aug 05, 2012 at 16:20 UTC | |
by nemesisgus (Acolyte) on Aug 05, 2012 at 16:31 UTC |