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)
In reply to Re: "my" slowing down programs?
by 1nickt
in thread "my" slowing down programs?
by jf1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |