Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: String concatenation

by Riales (Hermit)
on Apr 11, 2012 at 20:21 UTC ( #964615=note: print w/ replies, xml ) Need Help??


in reply to String concatenation

This is probably a case of unnecessary optimization, but here are benchmark results for those interested:

s/iter join dot interpolate1 interpolate +2 join 2.22 -- -24% -25% -32 +% dot 1.69 31% -- -1% -11 +% interpolate1 1.67 33% 1% -- -10 +% interpolate2 1.50 48% 13% 12% - +-

And here's the code that generated this. I had to loop a bunch to get any kind of result. Not sure if this has any kind of effect on anything.

use strict; use warnings; use Benchmark qw/:all/; my $string; my ($var1, $var2, $var3) = ('asdf', 'zxcv', 'qwer'); cmpthese(4, { dot => sub { $string = $var1 . $var2 . $var3 for (0 .. 10000000); }, interpolate1 => sub { $string = "${var1}${var2}${var3}" for (0 .. 10000000); }, interpolate2 => sub { $string = "$var1$var2$var3" for (0 .. 10000000); }, join => sub { $string = join('', $var1, $var2, $var3) for (0 .. 10000000); }, });


Comment on Re: String concatenation
Select or Download Code
Re^2: String concatenation
by chromatic (Archbishop) on Apr 11, 2012 at 20:31 UTC

    Both of the interpolation forms compile to the same optree. Perl does the same work for both of them. Any difference between their benchmarks is noise.

    Given that, the time difference between concatenation and interpolation (which do have different optrees) is insignificant.

      Ah, yes--I ran my script a couple more times and it seems that the two interpolates and the dot are pretty much the same but the join is consistently slower. I envy the monks with an understanding of what Perl does internally!

        I envy the monks with an understanding of what Perl does internally!
        You don't need an understanding of what Perl does internally to find out whether two code snippets compile to the same optree. Use -MO=Terse (or -MO=Concise).
      In fact it goes further than that; the dot form also compiles to the same optree as the interpolation forms.

      Dave.

        Hmmm.
        $ perl -MO=Terse -e '$v1.$v2.$v3' LISTOP (0x9dc5fe0) leave [1] OP (0x9dc6768) enter COP (0x9dd0650) nextstate BINOP (0x9dd06e0) concat [2] BINOP (0x9dd05b0) concat [1] UNOP (0x9dd0688) null [15] SVOP (0x9dd07e8) gvsv GV (0x9dcaa30) *v1 UNOP (0x9dd0578) null [15] SVOP (0x9dd0630) gvsv GV (0x9dcaa58) *v2 UNOP (0x9dd05d0) null [15] SVOP (0x9dcf9e0) gvsv GV (0x9dcaae4) *v3 -e syntax OK $ perl -MO=Terse -e '"$v1$v2$v3"' LISTOP (0x8b92778) leave [1] OP (0x8b88768) enter COP (0x8b92650) nextstate UNOP (0x8b926e0) null [67] OP (0x8b918d0) null [3] BINOP (0x8b925d0) concat [2] BINOP (0x8b925b0) concat [1] UNOP (0x8b92688) null [15] SVOP (0x8b927e8) gvsv GV (0x8b8ca44) *v1 UNOP (0x8b92578) null [15] SVOP (0x8b92630) gvsv GV (0x8b8cb34) *v2 UNOP (0x8b91920) null [15] SVOP (0x8b91a10) gvsv GV (0x8b8cae4) *v3 -e syntax OK
        Similar, but subtle differences.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://964615]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2014-10-20 14:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (77 votes), past polls