Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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

( #18934=categorized question: print w/ replies, xml ) Need Help??
Contributed by cbraga on Jun 20, 2000 at 08:15 UTC
Q&A  > subroutines

Answer: Is it computationally expensive to return a large string (50-100 kB) from a sub?
contributed by Russ

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)
Answer: Is it computationally expensive to return a large string (50-100 kB) from a sub?
contributed by gaal

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.

Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (5)
    As of 2016-08-28 11:09 GMT
    Find Nodes?
      Voting Booth?
      The best thing I ever won in a lottery was:

      Results (392 votes). Check out past polls.