Dear Perl monks, the other day I stumbled upon the issue that in a particular program declaring a variable with "my" actually slowed down program execution (for comparison I just repeated the very same instruction several times):
while (chomp ($line = <$gsr>)) { my @l1; @l1 = split $tab, $line; @l1 = split $tab, $line; @l1 = split $tab, $line; print(join($tab,@l1[@okc])); print(join($tab,@l1[@okc])); print(join($tab,@l1[@okc])); }
this leads to following timings:
73µs my@l1; 1.71s @l1 = split $tab, $line; 2.40s @l1 = split $tab, $line; 2.33s @l1 = split $tab, $line; 805ms print(join($tab,@l1[@okc])); 798ms print(join($tab,@l1[@okc])); 1.66s print(join($tab,@l1[@okc]));
i.e. the 2nd and following assignments seem to take more time than the 1st one. Could that be attributed to some additional overhead? It doesn't happen when omitting the "my" declaration or using "our" instead (see below). This is consistent across runs. If introducing "my" in the middle of a block the effect is the same (see at bottom). Using "local" instead of "my" seems not as expensive. But both "my" and "local" seem to slow down last write operation (is that some artifact? All measurments taken on Windows 7 with outpout redirected to a file).
235µs local @l1; 1.18s @l1 = split $tab, $line; 1.79s @l1 = split $tab, $line; 1.78s @l1 = split $tab, $line; 824ms print(join($tab,@l1[@okc])); 806ms print(join($tab,@l1[@okc])); 1.61s print(join($tab,@l1[@okc])); 92µs our @l1; 1.77s @l1 = split $tab, $line; 1.77s @l1 = split $tab, $line; 1.77s @l1 = split $tab, $line; 829ms print(join($tab,@l1[@okc])); 820ms print(join($tab,@l1[@okc])); 1.03s print(join($tab,@l1[@okc])); #my @l1; 1.78s @l1 = split $tab, $line; 1.77s @l1 = split $tab, $line; 1.77s @l1 = split $tab, $line; 810ms print(join($tab,@l1[@okc])); 805ms print(join($tab,@l1[@okc])); 1.01s print(join($tab,@l1[@okc])); 1.80s @l1 = split $tab, $line; 1.70s my @l1 = split $tab, $line; 2.41s @l1 = split $tab, $line; 807ms print(join($tab,@l1[@okc])); 794ms print(join($tab,@l1[@okc])); 1.65s print(join($tab,@l1[@okc]));

In reply to "my" slowing down programs? by jf1

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.