Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
on a Mac Pro, the array version takes 63 seconds. the reference version takes 22 seconds.#!/usr/bin/perl -w use strict; my $arraylength=1000; sub return_arrays { my @array; for (my $i=0; $i<$arraylength; ++$i) { push(@array, rand()); } return @array; ## or return \@array; } my $now=time(); my $sum=0; for (my $j=0; $j<100_000; ++$j) { my @v= return_arrays(); ## or my $v= $sum+= $v[ rand($arraylength) ]; ## of $v->[ } print "$sum\n"; print "Time: ".(time()-$now)." seconds\n";
my guess is that perl actually copies the array on a return, rather than just move pointers around. this seems somewhat inefficient.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Speed of Array Returns
by moritz (Cardinal) on Jan 30, 2010 at 23:06 UTC | |
|
Re: Speed of Array Returns
by jethro (Monsignor) on Jan 30, 2010 at 23:36 UTC | |
|
Re: Speed of Array Returns
by iaw4 (Monk) on Jan 30, 2010 at 23:11 UTC | |
by chromatic (Archbishop) on Jan 31, 2010 at 06:59 UTC | |
by bobr (Monk) on Jan 31, 2010 at 11:26 UTC | |
by iaw4 (Monk) on Feb 03, 2010 at 13:44 UTC | |
by chromatic (Archbishop) on Feb 03, 2010 at 20:47 UTC | |
|
Re: Speed of Array Returns
by ikegami (Patriarch) on Jan 31, 2010 at 17:12 UTC | |
|
Re: Speed of Array Returns
by Anonymous Monk on Jan 30, 2010 at 23:33 UTC |