in reply to My philosophy on coding (in Perl and other languages):
Which yeilds:use Benchmark; my @big_array = (1 .. 10000); sub ret_array { return @big_array; } sub ret_ref { return \@big_array; } timethese(10000, { 'return_array' => sub { my @array = ret_array(); }, 'return_ref' => sub { my $array_ref = ret_ref(); } });
Returning the reference is faster since it doesn't have to copy each element, allocate memory for it, etc. Granted it can make your code more cryptic, but in the cases where the returned array is large, or the subroutine is called often, the speed increase is well worth it.Benchmark: timing 10000 iterations of return_array, return_ref... return_array: 120 wallclock secs (117.82 usr + 1.65 sys = 119.47 CPU) return_ref: 0 wallclock secs ( 0.03 usr + 0.00 sys = 0.03 CPU)
/\/\averick
|
|---|