Like Perrin says, your benchmark functions aren't well suited to what you're testing. Your functions only look up the array to be sorted once per invocation; then you go on to sort the array and then assign the result into another variable. The cost of using a reference is going to be swamped by these later steps.

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:

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) } });
Here are the benchmark results on my system:
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.