in reply to "my" slowing down programs?

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.

Replies are listed 'Best First'.
Re^2: "my" slowing down programs?
by roboticus (Chancellor) on Aug 17, 2015 at 12:15 UTC

    1nickt:

    I somehow doubt that you're testing what you think you're testing ... unless your snipped about 10,000,000 lines out of your __DATA__ section...

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re^2: "my" slowing down programs?
by jf1 (Beadle) on Aug 17, 2015 at 08:29 UTC
    Maybe I should have mentioned several facts I omitted in the original post, sorry. Here comes some background information:
    1. The numbers reported were not single measurements, but resulted from an NYTProf run with the script processing the 1st 100 lines of a template of the file under consideration. I consider this to be enough for getting halfway stable results
    2. Any of the lines processed contains about 30.000 fields. With this number the difference does matter, belief me. In particular as the regular final input file size in production has a range of between 1e4 and 1e5 fields and around 1e6 lines. Thus even saving 10% or 20% of runtime is worth the effort
    3. Thanks for the suggestion of the Benchmark module. Nonetheless I'd rather not repeat the processing 500.000 times as this would mean about 12 days only for line splitting with limited gain of knowledge ;-)
    In production with external time measures at least processing seems faster with global than lexical variables. BTW: Using C or Pascal instead and coding splitting, filtering and joining by hand does not seem to gain much here (to be honest: almost nothing), not to mention using provided libraries, which seem to be even slower as they favor generic solutions. This only costs more of programmer's time.