in reply to Pass by ref vs alias : why does scalar size matter?

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.

Replies are listed 'Best First'.
Re^2: Pass by ref vs alias : why does scalar size matter?
by clinton (Priest) on May 02, 2008 at 08:51 UTC

    That's not the result I get. I've run your code 10 times, and while, yes, the actual numbers varied, ref was consistently 28-34% faster than alias.

    Clint