http://qs1969.pair.com?node_id=18953


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

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)