Usually I tend to back out when my own knowledge seems to differ when the majority, but just for the record, my understanding agrees with this guy's guru's. These two
while loops are not equivalent (and not for the reasons you think):
# within
while (my $x = $count) {
$count++;
last if $count >= 5000000;
}
# without
my $x;
while ($x = $count) {
$count++;
last if $count >= 5000000;
}
Benchmark: timing 10 iterations of within, without...
within: 40 wallclock secs (39.78 usr + 0.04 sys = 39.82 CPU) @ 0
+.25/s (n=10)
without: 35 wallclock secs (35.09 usr + 0.00 sys = 35.09 CPU) @ 0
+.28/s (n=10)
Now, I'm no expert on Perl internals, but it certainly
seems like
$x is being re-allocated for every loop. At a minimum,
$x falls out of scope when the loop ends, which leads me to believe it's falling out of scope for each iteration and being re-allocated. Note that it isn't being
redeclared, just
reallocated. I'd be interested if anyone has any other explanation...
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.