in reply to Show that using references is faster
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:
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.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% --
blokhead
|
|---|