It doesn't for me. The alias seems to be quicker. BUT ...

But the difference is fairly random. If I run the benchmark for a 10000 character long string 20 times, I get very different results. Here are the minimal and maximal rates:

max_alias => 213675
max_ref => 237530
min_alias => 156495
min_ref => 152439
and again when I ran it once more
max_alias => 237530
max_ref => 228833
min_alias => 160000
min_ref => 164204
As you can see, once the maximum and minimum is bigger for one, once for the other. All the speed difference is accidental.

use Benchmark qw(cmpthese); use strict; use warnings; print "\n","Length of string: 10000\n"; for (1..20) { my $original; $original.= chr(int(rand(128))) for 1..1000; $original = $original x 10; cmpthese (100000,{ ref => sub { my $new = $original; by_ref(\$new) }, alias => sub { my $new = $original; by_alias($new) }, }); } sub by_alias { $_[0] =~ s/\s+//; } sub by_ref { ${ $_[0] } =~ s/\s+//; }
my %data = (min_alias => 999999999, min_ref => 999999999); while (<>) { if (/^(\w+)\s+(\d+)\/s/) { my ($typ, $rate) = ($1,$2); $data{'min_'.$typ} = $rate if $data{'min_'.$typ} > $rate; $data{'max_'.$typ} = $rate if $data{'max_'.$typ} < $rate; } } print "$_ => $data{$_}\n" for sort keys %data;

Update: I have perl v5.8.7 running on Windows Vista.


In reply to Re: Pass by ref vs alias : why does scalar size matter? by Jenda
in thread Pass by ref vs alias : why does scalar size matter? by clinton

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.