First, if it's taking one to two seconds to split a line into an array, you have other problems! Also, you'll have to run your tests more than three times each to really benchmark things.

Maybe look at Devel::NYTProf and/or Benchmark.

#!/usr/bin/perl use strict; use warnings; use Benchmark qw[ timethese :hireswallclock]; sub with_my { while (<DATA>) { my @array = split; } } my @array2; sub without_my { while (<DATA>) { @array2 = split; } } for (1 ..5) { timethese (5000000, { with_my => 'with_my()', without_my => 'without_my()' }); } __DATA__ The quick brown fox jumps over the lazy dog.

The results seem to show that the difference is negligible, IMO.

Benchmark: timing 5000000 iterations of with_my, without_my... with_my: 11.0969 wallclock secs ( 6.75 usr + 3.42 sys = 10.17 CPU) + @ 491642.08/s (n=5000000) without_my: 10.6874 wallclock secs ( 6.84 usr + 3.41 sys = 10.25 CPU) + @ 487804.88/s (n=5000000) Benchmark: timing 5000000 iterations of with_my, without_my... with_my: 10.5256 wallclock secs ( 6.83 usr + 3.44 sys = 10.27 CPU) + @ 486854.92/s (n=5000000) without_my: 10.759 wallclock secs ( 7.02 usr + 3.52 sys = 10.54 CPU) +@ 474383.30/s (n=5000000) Benchmark: timing 5000000 iterations of with_my, without_my... with_my: 11.1877 wallclock secs ( 7.40 usr + 3.72 sys = 11.12 CPU) + @ 449640.29/s (n=5000000) without_my: 10.8896 wallclock secs ( 7.20 usr + 3.63 sys = 10.83 CPU) + @ 461680.52/s (n=5000000) Benchmark: timing 5000000 iterations of with_my, without_my... with_my: 11.0768 wallclock secs ( 7.33 usr + 3.69 sys = 11.02 CPU) + @ 453720.51/s (n=5000000) without_my: 10.9737 wallclock secs ( 7.28 usr + 3.65 sys = 10.93 CPU) + @ 457456.54/s (n=5000000) Benchmark: timing 5000000 iterations of with_my, without_my... with_my: 11.0938 wallclock secs ( 7.35 usr + 3.70 sys = 11.05 CPU) + @ 452488.69/s (n=5000000) without_my: 11.018 wallclock secs ( 7.29 usr + 3.69 sys = 10.98 CPU) +@ 455373.41/s (n=5000000)
The way forward always starts with a minimal test.

In reply to Re: "my" slowing down programs? by 1nickt
in thread "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.