Off the top of my head:

use Benchmark qw/cmpthese/; my $append = 'B' x 50; cmpthese (100, { dot => sub { my $c = 'A' x 50; $c = $c . $append f +or 1..1e5; }, dot_eq => sub { my $c = 'A' x 50; $c .= $append f +or 1..1e5; }, substr => sub { my $c = 'A' x 50; substr $c, length $c, 0, $append f +or 1..1e5; }, }); __END__ Rate substr dot dot_eq substr 16.5/s -- -35% -37% dot 25.5/s 55% -- -2% dot_eq 26.0/s 58% 2% --

Update: Erm, had to put the 1e5 inside each sub to reduce the overhead of the my declaration.

Update2: After playing with it for a while, I find interesting what happens when $append has a size that is a power of 2. Size of $c doesn't matter:

use Benchmark qw/cmpthese/; my $append = 'B' x 256; cmpthese (100, { dot => sub { my $c = 'A' x 50; $c = $c . $append f +or 1..1e5; }, dot_eq => sub { my $c = 'A' x 50; $c .= $append f +or 1..1e5; }, substr => sub { my $c = 'A' x 50; substr $c, length $c, 0, $append f +or 1..1e5; }, }); __END__ Rate substr dot_eq dot substr 6.99/s -- -35% -49% dot_eq 10.7/s 53% -- -21% dot 13.6/s 95% 27% --

Size of $c doesn't matter. So if you are working with 8 Kb chunks, you better use $data = $data . $more.

--
David Serrano


In reply to Re: simply appending to a scalar... by Hue-Bond
in thread simply appending to a scalar... by abachus

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



  • 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, details, 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, summary, 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.