Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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...

In reply to Re: redeclaring variables with 'my' by Fastolfe
in thread redeclaring variables with 'my' by tune

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-20 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found