sunshine_august has asked for the wisdom of the Perl Monks concerning the following question:

Hi, all monks:

I have a subroutin, which will return a scalar variable to its caller, but there may be several M bytes contained in the scalar variable, should I return the reference of this scalar variable or return the bare scalar variable for efficiency?

I write a benchmark, it shows that the reference edition is more efficient, but is that true? Following is my benchmark code:

#!/usr/bin/perl use strict; use warnings; use Benchmark qw(:all); sub caller_scalar { my $s = stub_scalar(); } sub caller_ref { my $s = stub_ref(); } sub stub_scalar { my $string = 'aaaaaaaaaaaaaaaaaaaaaaaa' x 1000000; return $string } sub stub_ref { my $string = 'aaaaaaaaaaaaaaaaaaaaaaaa' x 1000000; return \$string } cmpthese( 1000, { 'ref' => \&caller_ref, 'scalar' => \&caller_scalar, } );
here is the result:
Rate scalar ref scalar 10.9/s -- -20% ref 13.5/s 24% --

when Perl returns a scalar, does it just return the address of the scalar, or also copy the content of the scalar to the caller?

Replies are listed 'Best First'.
Re: Does the reference of a scalar more efficient than scalar?
by AnomalousMonk (Archbishop) on Dec 11, 2008 at 10:09 UTC
    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% --

      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?

Re: Does the reference of a scalar more efficient than scalar?
by ccn (Vicar) on Dec 11, 2008 at 09:24 UTC
    As said in Perl Cookbook

    Using references, you can efficiently pass large amounts of data to and from a subroutine.

    However, passing references to scalars typically turns out not to be an optimization at all.

    Yes, several megabytes contained in the returned value is a good reason to use references. Your benchmark proves it. However if the length of returned scalar is not big than you get valuable overhead on dereferencing.