Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > string concatentation vs. interpolation: which one is more optimal?

Reply
Thread Tools

string concatentation vs. interpolation: which one is more optimal?

 
 
rihad
Guest
Posts: n/a
 
      09-26-2007
Programming Perl (The Camel Book) states that "String concatenation is
also implied by the interpolation that happens in double-quoted
strings."

print $a . ' is equal to ' . $b . ".\n"; # dot operator
print "$a is equal to $b.\n"; # interpolation

Can someone experienced in Perl source code confirm that they are
identical in terms of raw speed and memory use or not? The second one
is much cooler, and if so, I'm going to get rid of concatenation
altogether. (I'm not even considering the list variant separated by
commas, which is suboptimal).

 
Reply With Quote
 
 
 
 
papahuhn
Guest
Posts: n/a
 
      09-26-2007
rihad schrieb:
> Programming Perl (The Camel Book) states that "String concatenation is
> also implied by the interpolation that happens in double-quoted
> strings."
>
> print $a . ' is equal to ' . $b . ".\n"; # dot operator
> print "$a is equal to $b.\n"; # interpolation
>
> Can someone experienced in Perl source code confirm that they are
> identical in terms of raw speed and memory use or not? The second one
> is much cooler, and if so, I'm going to get rid of concatenation
> altogether. (I'm not even considering the list variant separated by
> commas, which is suboptimal).
>


time perl -e '$a=$b=303; print "$a $b" for 1..39999999' > /dev/null
real 0m14.500s
user 0m14.460s
sys 0m0.022s

time perl -e '$a=$b=303; print $a." ".$b for 1..39999999' > /dev/null

real 0m12.111s
user 0m12.081s
sys 0m0.014s
 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      09-26-2007
On Sep 26, 1:16 pm, papahuhn <(E-Mail Removed)> wrote:
> rihad schrieb:
>
> > Programming Perl (The Camel Book) states that "String concatenation is
> > also implied by the interpolation that happens in double-quoted
> > strings."

>
> > print $a . ' is equal to ' . $b . ".\n"; # dot operator
> > print "$a is equal to $b.\n"; # interpolation

>
> > Can someone experienced in Perl source code confirm that they are
> > identical in terms of raw speed and memory use or not? The second one
> > is much cooler, and if so, I'm going to get rid of concatenation
> > altogether. (I'm not even considering the list variant separated by
> > commas, which is suboptimal).

>
> time perl -e '$a=$b=303; print "$a $b" for 1..39999999' > /dev/null
> real 0m14.500s
> user 0m14.460s
> sys 0m0.022s
>
> time perl -e '$a=$b=303; print $a." ".$b for 1..39999999' > /dev/null
>
> real 0m12.111s
> user 0m12.081s
> sys 0m0.014s


it probably depends on the length of the string as well. i just wanted
to add the third option of passing print a list:

bash-2.03$ time perl -e '$x=$y=303; print "$x $y" for 1..16777216' > /
dev/null

real 0m17.710s
user 0m17.590s
sys 0m0.002s



bash-2.03$ time perl -e '$x=$y=303; print $x." ".$y for 1..16777216'
> /dev/null


real 0m17.115s
user 0m17.001s
sys 0m0.053s



bash-2.03$ time perl -e '$x=$y=303; print $x," ",$y for 1..16777216'
> /dev/null


real 1m18.498s
user 1m18.310s
sys 0m0.073s

MUCH slower for some reason...thoughts?

 
Reply With Quote
 
Brian Wakem
Guest
Posts: n/a
 
      09-26-2007
papahuhn wrote:

> rihad schrieb:
>> Programming Perl (The Camel Book) states that "String concatenation is
>> also implied by the interpolation that happens in double-quoted
>> strings."
>>
>> print $a . ' is equal to ' . $b . ".\n"; # dot operator
>> print "$a is equal to $b.\n"; # interpolation
>>
>> Can someone experienced in Perl source code confirm that they are
>> identical in terms of raw speed and memory use or not? The second one
>> is much cooler, and if so, I'm going to get rid of concatenation
>> altogether. (I'm not even considering the list variant separated by
>> commas, which is suboptimal).
>>

>
> time perl -e '$a=$b=303; print "$a $b" for 1..39999999' > /dev/null
> real 0m14.500s
> user 0m14.460s
> sys 0m0.022s
>
> time perl -e '$a=$b=303; print $a." ".$b for 1..39999999' > /dev/null
>
> real 0m12.111s
> user 0m12.081s
> sys 0m0.014s



I found almost no difference:

$ time perl -e '$a=$b=303; print $a." ".$b for 1..39999999' > /dev/null

real 0m9.109s
user 0m9.095s
sys 0m0.012s

$ time perl -e '$a=$b=303; print "$a $b" for 1..39999999' > /dev/null

real 0m9.007s
user 0m8.997s
sys 0m0.010s



And as another poster has pointed out, using a list is very slow by
comparison:

$ time perl -e '$a=$b=303; print $a," ",$b for 1..39999999' > /dev/null

real 0m31.457s
user 0m31.438s
sys 0m0.018s


--
Brian Wakem
 
Reply With Quote
 
Mahesh Asolkar
Guest
Posts: n/a
 
      09-26-2007
On Sep 26, 9:42 am, rihad <(E-Mail Removed)> wrote:
> Programming Perl (The Camel Book) states that "String concatenation is
> also implied by the interpolation that happens in double-quoted
> strings."
>
> print $a . ' is equal to ' . $b . ".\n"; # dot operator
> print "$a is equal to $b.\n"; # interpolation
>
> Can someone experienced in Perl source code confirm that they are
> identical in terms of raw speed and memory use or not? The second one
> is much cooler, and if so, I'm going to get rid of concatenation
> altogether. (I'm not even considering the list variant separated by
> commas, which is suboptimal).


>From the benchmark below, it looks like interpolation and

concatenation come pretty close.

-------------------
#!/usr/bin/perl

use strict;
use warnings;
no warnings 'syntax';

use Benchmark qw 'cmpthese';

cmpthese 2000000 => {
'interp' => '$x=$y=303; $z = "$x $y"',
'concat' => '$x=$y=303; $z = $x . " " . $y',
'listjn' => '$x=$y=303; $z = $x," ",$y',
};

-------------- Results -------------------

Rate concat interp listjn
concat 1428571/s -- -1% -56%
interp 1438849/s 1% -- -56%
listjn 3278689/s 130% 128% --
------------------

I usually use one of the two that suits the most. When there is a
really long line involved, I tend to use the concatenation operator so
that I can break the line into multiple shorter lines. Interpolation
is fine in short lines.

HTH,
Mahesh.

 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      09-26-2007
On Sep 26, 3:46 pm, Mahesh Asolkar <(E-Mail Removed)> wrote:
> On Sep 26, 9:42 am, rihad <(E-Mail Removed)> wrote:
>
> > Programming Perl (The Camel Book) states that "String concatenation is
> > also implied by the interpolation that happens in double-quoted
> > strings."

>
> > print $a . ' is equal to ' . $b . ".\n"; # dot operator
> > print "$a is equal to $b.\n"; # interpolation

>
> > Can someone experienced in Perl source code confirm that they are
> > identical in terms of raw speed and memory use or not? The second one
> > is much cooler, and if so, I'm going to get rid of concatenation
> > altogether. (I'm not even considering the list variant separated by
> > commas, which is suboptimal).
> >From the benchmark below, it looks like interpolation and

>
> concatenation come pretty close.
>
> -------------------
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> no warnings 'syntax';
>
> use Benchmark qw 'cmpthese';
>
> cmpthese 2000000 => {
> 'interp' => '$x=$y=303; $z = "$x $y"',
> 'concat' => '$x=$y=303; $z = $x . " " . $y',
> 'listjn' => '$x=$y=303; $z = $x," ",$y',
>
> };
>
> -------------- Results -------------------
>
> Rate concat interp listjn
> concat 1428571/s -- -1% -56%
> interp 1438849/s 1% -- -56%
> listjn 3278689/s 130% 128% --
> ------------------
>


This is different than the tests the other posters conducted in that
you are not printing anything, and I don't think your listjn is doing
what you think it is.

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      09-26-2007
On Wed, 26 Sep 2007 09:42:11 -0700, rihad <(E-Mail Removed)> wrote:

>Subject: string concatentation vs. interpolation: which one is more optimal?


Something can't be "more optimal" than something else. Perhaps
"optimized"?

>print $a . ' is equal to ' . $b . ".\n"; # dot operator
>print "$a is equal to $b.\n"; # interpolation


You forget

print $a, ' is equal to ', $b, ".\n"; # print() takes a list

>Can someone experienced in Perl source code confirm that they are
>identical in terms of raw speed and memory use or not? The second one
>is much cooler, and if so, I'm going to get rid of concatenation
>altogether. (I'm not even considering the list variant separated by
>commas, which is suboptimal).


Do you *really* care?!? I agree that sometimes it *is* worth to look
at optimizations, but if you have performance problems, then the
printing of a short thing is highly likely *not* to be the cause of
them, and if it is, then you'd better switch to a faster language
altogether.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      09-26-2007
On Wed, 26 Sep 2007 12:46:32 -0700, Mahesh Asolkar <(E-Mail Removed)>
wrote:

>cmpthese 2000000 => {
> 'interp' => '$x=$y=303; $z = "$x $y"',
> 'concat' => '$x=$y=303; $z = $x . " " . $y',
> 'listjn' => '$x=$y=303; $z = $x," ",$y',


The last entry does something completely different altogether, because
the list is evaluated in scalar context, whereas in print() it's in
list context.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      09-26-2007

Quoth Michele Dondi <(E-Mail Removed)>:
> On Wed, 26 Sep 2007 09:42:11 -0700, rihad <(E-Mail Removed)> wrote:
>
> >Subject: string concatentation vs. interpolation: which one is more optimal?

>
> Something can't be "more optimal" than something else. Perhaps
> "optimized"?


Yes it can. 'more optimal' implies 'better, in an absolute sense',
whereas 'more optimized' would imply 'has been improved more'. That is,
if you have A which executes 10 times a second and B which executes 20
times, and you improve this to A executing 15 times and B executing 21
times, then A is 'more optimized' than B even though B is still 'more
optimal' than A. (Did that make any sense? )

> >print $a . ' is equal to ' . $b . ".\n"; # dot operator
> >print "$a is equal to $b.\n"; # interpolation

>
> You forget
>
> print $a, ' is equal to ', $b, ".\n"; # print() takes a list


He(?) mentioned that he had already discarded this case.

> >Can someone experienced in Perl source code confirm that they are
> >identical in terms of raw speed and memory use or not? The second one
> >is much cooler, and if so, I'm going to get rid of concatenation
> >altogether. (I'm not even considering the list variant separated by
> >commas, which is suboptimal).

>
> Do you *really* care?!? I agree that sometimes it *is* worth to look
> at optimizations, but if you have performance problems, then the
> printing of a short thing is highly likely *not* to be the cause of
> them, and if it is, then you'd better switch to a faster language
> altogether.


While I couldn't agree more, in the interests of answering the question
asked:

~% perl -MO=Concise -e'$a . " foo " . $b'
8 <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) v ->3
7 <2> concat[t2] vKS/2 ->8
5 <2> concat[t1] sK/2 ->6
- <1> ex-rv2sv sK/1 ->4
3 <$> gvsv(*a) s ->4
4 <$> const(PV " foo ") s ->5
- <1> ex-rv2sv sK/1 ->7
6 <$> gvsv(*b) s ->7
-e syntax OK
~% perl -MO=Concise -e'"$a foo $b"'
8 <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) v ->3
- <1> ex-stringify vK/1 ->8
- <0> ex-pushmark s ->3
7 <2> concat[t2] sKS/2 ->8
5 <2> concat[t1] sK/2 ->6
- <1> ex-rv2sv sK/1 ->4
3 <$> gvsv(*a) s ->4
4 <$> const(PV " foo ") s ->5
- <1> ex-rv2sv sK/1 ->7
6 <$> gvsv(*b) s ->7
-e syntax OK

The ex-foo ops are those that have been optimized away, so they compile
to exactly the same optree, so have exactly the same performance. Any
differences found elsewhere in the thread are errors in benchmarking.

Ben

 
Reply With Quote
 
Mahesh Asolkar
Guest
Posts: n/a
 
      09-26-2007
On Sep 26, 1:41 pm, Michele Dondi <(E-Mail Removed)> wrote:
> On Wed, 26 Sep 2007 12:46:32 -0700, Mahesh Asolkar <(E-Mail Removed)>
> wrote:
>
> >cmpthese 2000000 => {
> > 'interp' => '$x=$y=303; $z = "$x $y"',
> > 'concat' => '$x=$y=303; $z = $x . " " . $y',
> > 'listjn' => '$x=$y=303; $z = $x," ",$y',

>
> The last entry does something completely different altogether, because
> the list is evaluated in scalar context, whereas in print() it's in
> list context.
>


Michele and it_says_BALLS_on_your forehead, yes listjn is really quite
different from what the OP wanted. I was trying to isolate performance
of concatenation and interpolation operations from the IO overhead of
print - if at all. I did not pay attention on the side effect on
listjn. My apologies.

/Mahesh.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can one declare more than one signal on one line? Merciadri Luca VHDL 4 11-01-2010 02:00 PM
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
which is better, string concatentation or substitution? John Salerno Python 16 05-12-2006 05:53 PM
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM
Concatentation Daniel Bergquist Perl Misc 4 07-14-2004 02:16 PM



Advertisments