in reply to Curious Perl Behavior...
The problem is that you are including the time taken to construct the original (anonymous) array.
Try this and see how you get on (the timings shown are AS 5.10.1 under Vista 64-bit):
#!/user/bin/perl use strict; use warnings; use Benchmark qw[ cmpthese ]; sub sum_copy { my @numbers = @_; my $sum = 0; foreach my $num (@numbers){ $sum += $num } $sum; } sub sum_ref { my ($numbers_ref) = @_; my $sum = 0; foreach my $num (@$numbers_ref){ $sum += $num }; $sum; } our $N //= 1e6; my @numbers = 1 .. $N; cmpthese -3, { copy => sub{ my $sum = sum_copy( @numbers ); }, ref => sub{ my $sum = sum_ref( \@numbers ); }, }; __END__ C:\test>junk28 -N=1e4 Rate copy ref copy 3.92/s -- -59% ref 9.67/s 146% -- C:\test>junk28 -N=1e5 Rate copy ref copy 3.95/s -- -59% ref 9.62/s 144% -- C:\test>junk28 -N=1e6 Rate copy ref copy 3.92/s -- -59% ref 9.66/s 146% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Curious Perl Behavior...
by ack (Deacon) on May 26, 2010 at 15:23 UTC |