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


in reply to append to a string or make an array? (memory and time efficiency)

I'm not sure that this is exactly a great benchmark, but it seems fairly clear that appending to a string is a generally a bit quicker than pushing to an array.

#! perl -slw use strict; use Benchmark qw[cmpthese]; cmpthese( -3, { append1k => q[ my $buffer .= 'x' x 1024 for 0..5; ], push1k => q[ my @buffer; push @buffer, 'X' x 1024 for 0..5; ], }); cmpthese( -3, { append8k => q[ my $buffer .= 'x' x 8192 for 0..5; ], push8k => q[ my @buffer; push @buffer, 'X' x 8192 for 0..5; ], }); cmpthese( -3, { append64k => q[ my $buffer .= 'x' x 65536 for 0..5; ], push64k => q[ my @buffer; push @buffer, 'X' x 65536 for 0..5; ], });
P:\test>test Rate push1k append1k push1k 4816/s -- -26% append1k 6472/s 34% -- Rate push8k append8k push8k 982/s -- -9% append8k 1078/s 10% -- Rate push64k append64k push64k 43.4/s -- -33% append64k 65.3/s 50% --

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller