The big performance win from references comes from being able to pass them in and out of subroutines more efficiently. This benchmark illustrates the benefit more clearly:
Here are the benchmark results on my system:use strict; use Benchmark; my @x = (0..100_000); sub pass_by_value(@) { my $x; foreach (@_) { $x += $_ } return $x + } sub pass_by_reference(\@) { my $x; foreach (@$_) { $x += $_ } return $ +x } timethese(500, { 'Pass by Ref' => sub { pass_by_reference(@x) }, 'Pass by Value' => sub { pass_by_value(@x) } }); sub return_by_value(@) { return @x }; sub return_by_reference(\@) { return \@x }; timethese(500, { 'Return by Ref' => sub { return_by_reference(@x) }, 'Return by Value' => sub { return_by_value(@x) } });
Benchmark: timing 500 iterations of Pass by Ref, Pass by Value...
Pass by Ref: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU)
(warning: too few iterations for a reliable count)
Pass by Value: 78 wallclock secs (67.48 usr + 0.17 sys = 67.65 CPU) @ 7.39/s (n=500)
Benchmark: timing 500 iterations of Return by Ref, Return by Value...
Return by Ref: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU)
(warning: too few iterations for a reliable count)
Return by Value: 16 wallclock secs (14.19 usr + 0.03 sys = 14.22 CPU) @ 35.16/s (n=500)
In reply to Re: Performance of Perl references
by kjherron
in thread Performance of Perl references
by fuzzyping
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |