Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Is it computationally expensive to return a large string (50-100 kB) from a sub?

by cbraga (Pilgrim)
on Jun 20, 2000 at 08:15 UTC ( #18934=perlquestion: print w/replies, xml ) Need Help??

cbraga has asked for the wisdom of the Perl Monks concerning the following question: (subroutines)

Is it computationally expensive to return a large string (50-100 kB) from a sub?

Originally posted as a Categorized Question.

  • Comment on Is it computationally expensive to return a large string (50-100 kB) from a sub?

Replies are listed 'Best First'.
Re: Is it computationally expensive to return a large string (50-100 kB) from a sub?
by Russ (Deacon) on Jun 20, 2000 at 12:09 UTC
    It is far more efficient to return a reference to a large string than to return the string itself. Here are my benchmarks:
    sub Str { my $Str = q{ # Here follows about 100K of ASCII data }; $Str; } timethese(10000, { Str => q{ my $R = Str() } }); Benchmark: timing 10000 iterations of Str... Str: 29 wallclock secs (28.64 usr + 0.01 sys = 28.65 CPU)
    sub Ref { my $Str = q{ # Here follows the same 100K of ASCII data }; \$Str; } timethese(10000, { Ref => q{ my $R = Ref() } }); Benchmark: timing 10000 iterations of Ref... Ref: 9 wallclock secs (10.50 usr + 0.00 sys = 10.50 CPU)
Re: Is it computationally expensive to return a large string (50-100 kB) from a sub?
by gaal (Parson) on Oct 30, 2004 at 07:32 UTC
    However, when passing large strings as arguments to a sub, perl internally puts a pointer to the string on the stack (rather than the value itself). Using references (and later accessing the data indirectly) can actually be slower than passing the original string, but the difference is much smaller than that from the example above.

    Note, though, that this doesn't mean you should flatten non-scalar data when designing your subs. When passing an array or a hash with many members, use a reference.

      Oddly enough, you seem to be correct. Passing a 100 kb string and then assigning its first 5 characters to a another string 10,000,000 times took 17 seconds via a regular pass and 22 seconds when passing by reference. However, updating the first 5 characters of the string via reference only took 26 seconds for the same 10,000,000 iterations, while updating and passing back without references took 28 seconds for only 300,000 iterations. Of course, it took 20 seconds for 10,000,000 iterations when updated and not passed back, so the bottom line is:

      If you want to use a string but not change it OUTSIDE OF THE SUB's SCOPE, pass without references:
      (EDIT: See below - you can even change the item without references)


      If you want to update a string during its pass to a sub, pass as reference:


      EDIT: From the CB:

      <tilly> Here is a function that swaps the first two variables passed in: sub swap {@_[0,1] = @_[1,0];}
      <tilly> Perl is pass by reference, return by value, copy by value.
      <TedPride> Odd. So as long as you reference via the default input/output array, you can update even if something isn't passed via reference.
      <tilly> @_ contains aliases to the passed in values. As soon as you copy it out into local variables you've copied by value. If you call substr directly on the contents of @_ it is faster than having to create and undo references.

      So you can even update items that haven't been passed via reference, as long as they stay inside @_. One wonders now why anyone uses references...

      EDIT: That should read, "One wonders why anyone uses references when passing data to subs." References are useful for keeping track of anonymous subs, for pointing to sections of nested structures, and so on.

      Uh, I think that there was a copy/paste error in entering this question...

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://18934]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2022-01-23 20:56 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (64 votes). Check out past polls.