in reply to Does the reference of a scalar more efficient than scalar?

The comparison code of the OP also includes the time taken to generate a large scalar for each test iteration. Stripped-down code makes the point more forcefully.
use strict; use warnings; use Benchmark qw(cmpthese); my $long_string = 'aaaaaaaaaaaaaaaaaaaaaaaa' x 1000000; my $destination; sub return_scalar { return $long_string } sub return_ref { return \$long_string } cmpthese( -1, { 'scalar' => sub { $destination = return_scalar() }, 'ref' => sub { $destination = return_ref () }, } );
Result:
Rate scalar ref scalar 22.2/s -- -100% ref 1612824/s 7250890% --

Replies are listed 'Best First'.
Re^2: Does the reference of a scalar more efficient than scalar?
by Tanktalus (Canon) on Dec 11, 2008 at 17:02 UTC

    That's kind of what I was expecting, though I have to admit some disappointment that it's true. I was hoping that perl would use copy-on-write semantics for scalars which would have both of these take the same time (pretty much). Does anyone know if there's an affirmative reason for this (i.e., "We don't do it because X, Y, and Z" as opposed to "We never thought about it" or "No time to bother")? How about for perl 6?