At 08:28 2002.01.08, Prahlad Vaidyanathan wrote: >Hi, > >Was wondering which of these 2 pieces of code is more efficient : > ><code1> > my $file ; > foreach $file (@tarred) { #do something ; } > foreach $file (@gzipped) { #do something else ; } ></code1> > > OR > ><code2> > foreach my $file (@tarred) { #do something ; } > foreach my $file (@gzipped) { #do something else ; } ></code2>
The best way to find out is to time it. I wrote a little test script and with the help of the Benchmark module I though it would be fun to find out. I've add a test with our instead of my and another with use vars (both global version of the iteration variable). See the code at the end. Putting the my before the two loop or within each foreach doesn't make much a difference. Depending on the number of times I try it, sometime one is faster and sometime it's the other. What really surprised me was that using variable with a global scope is slower (around 4 percent). I've also put the full results at the end. I was wondering if someone could explain why a variable define with a global scope was slower that one define within the local lexical scope when used in a loop? Maybe it's my test script that has a problem? Is there other tools that can be used to understand what is happening here? Thanks for the help <code "test.pl"> #!perl use strict; use warnings; use Benchmark; #Generating lists my @mylist1 = ( 1 .. 30000 ); my @mylist2 = ( 1 .. 10000 ); # One global variable for a test case our $testiter; # Another global variable use vars qw($testiter2); $testiter2 = 0; # Let benchmark for a lot differents counts (lots of variable creations) my @mycount = ( 100, 200, 300, 500, 800, 1000, 2000); # Prototype (I like them) sub mybench($); foreach my $count (@mycount) { mybench($count); print "\n"; } sub mybench($) { my $count = shift; Benchmark::cmpthese($count, { 'One my' => sub { my $total = 0; my $testiter; foreach $testiter (@mylist1) { # First loop, useless operation just for test $total += $testiter; } foreach $testiter (@mylist2) { # Second loop, useless operation just for test $total += $testiter; } }, 'No my (our)' => sub { my $total = 0; foreach $testiter (@mylist1) { # First loop, useless operation just for test $total += $testiter; } foreach $testiter (@mylist2) { # Second loop, useless operation just for test $total += $testiter; } }, 'No my (use vars)' => sub { my $total = 0; foreach $testiter2 (@mylist1) { # First loop, useless operation just for test $total += $testiter2; } foreach $testiter2 (@mylist2) { # Second loop, useless operation just for test $total += $testiter2; } }, 'Each his my' => sub { my $total = 0; foreach my $testiter (@mylist1) { # First loop, useless operation just for test $total += $testiter; } foreach my $testiter (@mylist2) { # Second loop, useless operation just for test $total += $testiter; } }, }); } </code> <result "from test.pl"> Benchmark: timing 100 iterations of Each his my, No my (our), No my (use vars), One my... Each his my: 5 wallclock secs ( 5.46 usr + 0.00 sys = 5.46 CPU) @ 18.32/s (n=100) No my (our): 6 wallclock secs ( 5.60 usr + 0.00 sys = 5.60 CPU) @ 17.86/s (n=100) No my (use vars): 6 wallclock secs ( 5.64 usr + 0.00 sys = 5.64 CPU) @ 17.74/s (n=100) One my: 5 wallclock secs ( 5.44 usr + 0.00 sys = 5.44 CPU) @ 18.39/s (n=100) Rate No my (use vars) No my (our) Each his my One my No my (use vars) 17.7/s -- -1% -3% -4% No my (our) 17.9/s 1% -- -3% -3% Each his my 18.3/s 3% 3% -- -0% One my 18.4/s 4% 3% 0% -- Benchmark: timing 200 iterations of Each his my, No my (our), No my (use vars), One my... Each his my: 11 wallclock secs (10.86 usr + 0.00 sys = 10.86 CPU) @ 18.42/s (n=200) No my (our): 12 wallclock secs (11.30 usr + 0.02 sys = 11.32 CPU) @ 17.67/s (n=200) No my (use vars): 11 wallclock secs (11.34 usr + 0.00 sys = 11.34 CPU) @ 17.64/s (n=200) One my: 11 wallclock secs (10.83 usr + 0.01 sys = 10.84 CPU) @ 18.46/s (n=200) Rate No my (use vars) No my (our) Each his my One my No my (use vars) 17.6/s -- -0% -4% -4% No my (our) 17.7/s 0% -- -4% -4% Each his my 18.4/s 4% 4% -- -0% One my 18.5/s 5% 4% 0% -- Benchmark: timing 300 iterations of Each his my, No my (our), No my (use vars), One my... Each his my: 16 wallclock secs (16.29 usr + 0.00 sys = 16.29 CPU) @ 18.41/s (n=300) No my (our): 17 wallclock secs (16.94 usr + 0.01 sys = 16.95 CPU) @ 17.69/s (n=300) No my (use vars): 17 wallclock secs (16.98 usr + 0.00 sys = 16.98 CPU) @ 17.66/s (n=300) One my: 17 wallclock secs (16.29 usr + 0.01 sys = 16.30 CPU) @ 18.40/s (n=300) Rate No my (use vars) No my (our) One my Each his my No my (use vars) 17.7/s -- -0% -4% -4% No my (our) 17.7/s 0% -- -4% -4% One my 18.4/s 4% 4% -- -0% Each his my 18.4/s 4% 4% 0% -- Benchmark: timing 500 iterations of Each his my, No my (our), No my (use vars), One my... Each his my: 27 wallclock secs (27.54 usr + 0.01 sys = 27.55 CPU) @ 18.15/s (n=500) No my (our): 29 wallclock secs (28.25 usr + 0.00 sys = 28.25 CPU) @ 17.70/s (n=500) No my (use vars): 28 wallclock secs (28.33 usr + 0.00 sys = 28.33 CPU) @ 17.65/s (n=500) One my: 27 wallclock secs (27.14 usr + 0.00 sys = 27.14 CPU) @ 18.42/s (n=500) Rate No my (use vars) No my (our) Each his my One my No my (use vars) 17.6/s -- -0% -3% -4% No my (our) 17.7/s 0% -- -2% -4% Each his my 18.1/s 3% 3% -- -1% One my 18.4/s 4% 4% 2% -- Benchmark: timing 800 iterations of Each his my, No my (our), No my (use vars), One my... Each his my: 44 wallclock secs (43.45 usr + 0.02 sys = 43.47 CPU) @ 18.40/s (n=800) No my (our): 46 wallclock secs (45.97 usr + 0.00 sys = 45.97 CPU) @ 17.40/s (n=800) No my (use vars): 46 wallclock secs (45.22 usr + 0.00 sys = 45.22 CPU) @ 17.69/s (n=800) One my: 43 wallclock secs (43.47 usr + 0.01 sys = 43.48 CPU) @ 18.40/s (n=800) Rate No my (our) No my (use vars) One my Each his my No my (our) 17.4/s -- -2% -5% -5% No my (use vars) 17.7/s 2% -- -4% -4% One my 18.4/s 6% 4% -- -0% Each his my 18.4/s 6% 4% 0% -- Benchmark: timing 1000 iterations of Each his my, No my (our), No my (use vars), One my... Each his my: 55 wallclock secs (54.25 usr + 0.00 sys = 54.25 CPU) @ 18.43/s (n=1000) No my (our): 57 wallclock secs (56.55 usr + 0.01 sys = 56.56 CPU) @ 17.68/s (n=1000) No my (use vars): 56 wallclock secs (56.55 usr + 0.02 sys = 56.57 CPU) @ 17.68/s (n=1000) One my: 55 wallclock secs (54.23 usr + 0.01 sys = 54.24 CPU) @ 18.44/s (n=1000) Rate No my (use vars) No my (our) Each his my One my No my (use vars) 17.7/s -- -0% -4% -4% No my (our) 17.7/s 0% -- -4% -4% Each his my 18.4/s 4% 4% -- -0% One my 18.4/s 4% 4% 0% -- Benchmark: timing 2000 iterations of Each his my, No my (our), No my (use vars), One my... Each his my: 109 wallclock secs (108.59 usr + 0.01 sys = 108.60 CPU) @ 18.42/s (n=2000) No my (our): 113 wallclock secs (113.10 usr + 0.04 sys = 113.14 CPU) @ 17.68/s (n=2000) No my (use vars): 114 wallclock secs (113.11 usr + 0.04 sys = 113.15 CPU) @ 17.68/s (n=2000) One my: 109 wallclock secs (108.74 usr + 0.01 sys = 108.75 CPU) @ 18.39/s (n=2000) Rate No my (use vars) No my (our) One my Each his my No my (use vars) 17.7/s -- -0% -4% -4% No my (our) 17.7/s 0% -- -4% -4% One my 18.4/s 4% 4% -- -0% Each his my 18.4/s 4% 4% 0% -- </result> ----------------------------------------------------------- Éric Beaudoin <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]