in reply to Show that using references is faster
Your benckmark is flawed.
The point to passing references is that you can group multiple things together. The return values and arguments to functions all go on the stack as a list. The less impact you have on this stack, the less pushing/popping work perl will have to do. Less work == faster. In your example you do not vary the number of parameters appropriately.
Smaller argument lists are faster
use Benchmark 'cmpthese'; my @list = 1 .. 1_000_000; $result = 0; cmpthese( 0, { list => sub { sub { $result = scalar @_ }->( @list ) }, refr => sub { sub { $result = scalar @_ }->( \ @list ) } } ); # Rate list refr # list 20.2/s -- -100% # refr 796087/s 3947995% --
Smaller return lists are faster
use Benchmark 'cmpthese'; @list = 1 .. 1_000_000; @result = @list; $result = \ @list; cmpthese( 0, { list => sub { @result = sub { @list }->() }, refr => sub { $result = sub { \ @list }->() } } ); # Rate list refr # list 1.67/s -- -100% # refr 774198/s 46258208% --
|
|---|