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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re: append to a string or make an array? (memory and time efficiency) by BrowserUk
in thread append to a string or make an array? (memory and time efficiency) by swkronenfeld

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2023-01-30 08:25 GMT
Find Nodes?
    Voting Booth?

    No recent polls found