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

Answer: 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 ( #18953=categorized answer: print w/replies, xml ) Need Help??

Q&A > subroutines > Is it computationally expensive to return a large string (50-100 kB) from a sub? - Answer 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)

  • Comment on Answer: Is it computationally expensive to return a large string (50-100 kB) from a sub?
  • Select or Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-07-25 23:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (383 votes). Check out past polls.