in reply to Show that using references is faster

Your sort is almost certainly the dominant part of the runtime. The sort takes O(n log n) time, while passing an array takes O(n) time, so the sort will overshadow differences in parameter passing as the inputs get larger. I'd use an operation inside the sub that also runs in linear time (like summing the elements).

There's also the fact that I'm pretty sure that @{$_[0]} as an argument to sort ends up making a copy of the array anyway. I'd avoid using the passed array in list context at all within your benchmark. Access the items by their indices to avoid any unwanted copying:

use Benchmark 'cmpthese'; my @data = map { rand } 0 .. 4000; cmpthese(-2, { copy => sub { copy(@data) }, ref => sub { ref(\@data) } }); sub copy { my $sum; $sum += $_[$_] for 0 .. $#_; return $sum +} sub ref { my $sum; $sum += $_[0][$_] for 0 .. $#{$_[0]}; return $sum +} __END__ Rate copy ref copy 370/s -- -100% ref 1884708/s 508700% --
Update: Fixed really dumb error (didn't pass anything to the subs). Code reflects changes (and copy vs. ref is an even bigger difference than before). Thanks bmann.

blokhead