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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.