Generic print concatenation question

Generic print concatenation question

am 22.10.2008 12:28:05 von aw

Hi.

This is probably a question better asked to the perl monks or similar,
but if there are any of them lurking around here, it would save me a
subscription.

In various programs, I do a lot of printing, for results or for logging.
Really many print statements, in forms such as :

print OUT "$var1 some fixed string [$var2] another string\n";
OR
print OUT $var1,"some fixed string [",$var2,"] another string","\n";
OR
print OUT $var1 . "some fixed string [" . $var2 . "] another string" . "\n";
and other variations.

Some of these forms are easier to handle and type than others, depending
a bit on circusmtances. In many cases for instance, $var1 above is
always the same string (a prefix of some kind common to all print's or
many of them in a row), but $var2 is different each time (both the
specific variable and its content).
The individual print statements cannot be grouped together as one print,
they happen at different places/times.

Now, my question is, considering the way perl handles these things
internally, how do these different forms compare in terms of eficiency ?

I would not ask, if there were not sometimes really many of these being
executed over and over again. I figure it may be worth knowing if one
of the forms above (or another one I haven't tried) is really better
than others in the sense that it saves magabytes of memory or cumulated
seconds of processing time.

Thanks in advance,
André

Re: Generic print concatenation question

am 22.10.2008 12:48:49 von Clinton Gormley

> This is probably a question better asked to the perl monks or similar,
> but if there are any of them lurking around here, it would save me a
> subscription.

it's a subscription worth having :) I've learnt more about Perl since
I've been there than in the preceding decade.

> Now, my question is, considering the way perl handles these things
> internally, how do these different forms compare in terms of eficiency ?

Perl sorts all of this out at compile time, so:

print "$var1 text"
and
print $var1.' text'

are represented by exactly the same opcodes:

perl -MO=Concise -e 'my $var = 'abc'; print $var .q{ text}'

c <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) v:{ ->3
5 <2> sassign vKS/2 ->6
3 <$> const[PV "abc"] s/BARE ->4
4 <0> padsv[$var:1,2] sRM*/LVINTRO ->5
6 <;> nextstate(main 2 -e:1) v:{ ->7
b <@> print vK ->c
7 <0> pushmark s ->8
a <2> concat[t2] sK/2 ->b
8 <0> padsv[$var:1,2] s ->9
9 <$> const[PV " text"] s ->a


perl -MO=Concise -e 'my $var = 'abc'; print "$var text"'

c <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) v:{ ->3
5 <2> sassign vKS/2 ->6
3 <$> const[PV "abc"] s/BARE ->4
4 <0> padsv[$var:1,2] sRM*/LVINTRO ->5
6 <;> nextstate(main 2 -e:1) v:{ ->7
b <@> print vK ->c
7 <0> pushmark s ->8
- <1> ex-stringify sK/1 ->b
- <0> ex-pushmark s ->8
a <2> concat[t2] sK/2 ->b
8 <0> padsv[$var:1,2] s ->9
9 <$> const[PV " text"] s ->a


clint

Re: Generic print concatenation question

am 22.10.2008 18:37:49 von David Nicol

T24gV2VkLCBPY3QgMjIsIDIwMDggYXQgNToyOCBBTSwgQW5kcsOpIFdhcm5p ZXIgPGF3QGljZS1z
YS5jb20+IHdyb3RlOgoKPiBhbm90aGVyIG9uZSBJIGhhdmVuJ3QgdHJpZWQK CmhvdyBhYm91dCBh
biBJbmxpbmU6OkMgZnVuY3Rpb24gdGhhdCB3cmFwcyBwcmludGYgYW5kIHRh a2VzICR2YXIyIGFz
CmFuIGFyZ3VtZW50LCBhbmQgYW5vdGhlciB0aGF0IHRha2VzICR2YXIxIGFz IGFuIGFyZ3VtZW50
IHdoaWNoCm92ZXJ3cml0ZXMgdGhlIHN0YXRpYyBidWZmZXIgYWxsb2NhdGVk IGZvciB0aGUgZm9y
bWF0IHN0cmluZz8K

Re: Generic print concatenation question

am 22.10.2008 19:21:27 von Perrin Harkins

On Wed, Oct 22, 2008 at 6:28 AM, Andr=E9 Warnier wrote:
> I would not ask, if there were not sometimes really many of these being
> executed over and over again. I figure it may be worth knowing if one of
> the forms above (or another one I haven't tried) is really better than
> others in the sense that it saves magabytes of memory or cumulated second=
s
> of processing time.

If you run your app through Devel::NYTProf, you won't have to guess.

- Perrin