in reply to simply appending to a scalar...

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

Replies are listed 'Best First'.
Re^2: simply appending to a scalar...
by chromatic (Archbishop) on Jun 25, 2006 at 14:52 UTC
    So if you are working with 8 Kb chunks...

    ... you're doing IO and microbenchmarks of Perl operators really don't matter. If you really need all the speed you can get, and if you've already optimized the rest of the slow parts in your program, tune your buffer size.

Re^2: simply appending to a scalar...
by ikegami (Patriarch) on Jun 25, 2006 at 15:04 UTC

    How many times did you run the 256 one? dot_eq equals dot for me every time.

    ActivePerl 5.8.0 on WinXP.

      How many times did you run the 256 one?

      Before reading your post, 3 times; now 6. Always same results:

      substr: >6.80 dot_eq: 10.2 .. 10.7 dot: 13.3 .. 13.6

      This is perl, v5.8.8 built for i486-linux-gnu-thread-multi on Debian/Linux.

      --
      David Serrano