I don't know where you get a 50% hit for the 'my' inside the for loop. When I do this:
$a_huge_value = 3200000; my($foo); $time1 = time(); for ($i = 0; $i < $a_huge_value; $i++) { $foo = getValue(); codeThatUsesFoo($foo); } $time2 = time(); for ($i = 0; $i < $a_huge_value; $i++) { my $foo = getValue(); codeThatUsesFoo($foo); } $time3 = time(); print $time2 - $time1, "\n"; print $time3 - $time2, "\n"; sub getValue { return $_[0]; } sub codeThatUsesFoo { return $_[0]; }
I get:
[bobn@trc2:/home/bobn/misc]# perl my2.pl 14 14 [bobn@trc2:/home/bobn/misc]# perl my2.pl 13 15 [bobn@trc2:/home/bobn/misc]# perl my2.pl 13 15 [bobn@trc2:/home/bobn/misc]# perl my2.pl 14 16
Changing the subs to:
sub getValue { return } sub codeThatUsesFoo { return }
yields:
[bobn@trc2:/home/bobn/misc]# perl my2.pl 12 12 [bobn@trc2:/home/bobn/misc]# perl my2.pl 12 13 [bobn@trc2:/home/bobn/misc]# perl my2.pl 13 13 [bobn@trc2:/home/bobn/misc]# perl my2.pl 12 13
SO the decision is really based on what it *should* be based on - is $foo intended to be used outside the loop, with the effects of the loop desired to be visible. If so, do it the first way. If not, do it the second way.

--Bob Niederman, http://bob-n.com

All code given here is UNTESTED unless otherwise stated.


In reply to Re: Two Questions on "my" by bobn
in thread Two Questions on "my" by C_T

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.