Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Concatenate or Join?

by tobyink (Abbot)
on Nov 12, 2012 at 20:54 UTC ( #1003507=note: print w/ replies, xml ) Need Help??


in reply to Re: Concatenate or Join?
in thread Concatenate or Join?

It depends what you're doing really. In your example, join beats concat, but that's because you are looping through an array. Here though, concatenation beats it (albeit only by about 10%):

use strict; use warnings; use Benchmark qw{ cmpthese }; sub foo { 'abc' }; sub bar { 'def' }; cmpthese( -3, { concat => sub { my $ret = foo().bar(); return $ret; }, join => sub { my $ret = join q{}, foo(), bar(); return $ret; }, });

And here (thanks to the Perl compiler being so good at constant folding) concatenation is more than twice as fast as join:

use strict; use warnings; use Benchmark qw{ cmpthese }; sub foo () { 'abc' }; sub bar () { 'def' }; cmpthese( -3, { concat => sub { my $ret = foo . bar; return $ret; }, join => sub { my $ret = join q{}, foo, bar; return $ret; }, });
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re^2: Concatenate or Join?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2014-09-30 14:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (372 votes), past polls