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

B

brian d foy

Helmut Wollmersdorfer said:
Ben Morrow wrote:
Both are meaningless.

I always try to exchange 'optimize' by 'maximize' or 'minimize', e.g.

- minimize runtime
- minimize memory usage
- maximize execution rate


well, minimize and maximize work well for single dimensions, but when
you get into multi-dimensional concerns (say, memoery and speed), you
can't use those terms that well anymore. In the memory-speed surface
there will points where one or the other dimension are better, some
local minimums, and so on, but the optimal solution might not be the
one that has the absolute minumum for either of the factors.

I like "optimal" still, but people have to realize that it's a relative
and subjective term based on the assumptions and goals of the person
using it. It's not meaningless although it is often used without
context and with the implicit asumptions that everyone wants the same
thing. :)
 
R

rihad

When you start looking at the speed of things like this and actaully
caring about the answer, you'll probably find that Perl is not the
right tool for the job. Perl is really fast, but if this sort of
operation is too slow for you, then the bigger tasks in Perl, uch as
using a module, will just kill your performance (relatively).
Hi, brian d foy (I like the way your name is spelled that makes you
stand out from the rest throughout "Learning Perl" and the Perl FAQ -
it must be you really deserve this distinction :))

It's not that I want Perl to be as fast as assembly, no. I just don't
want to be using suboptimal patterns from the start if it can be done
better AND if the loss of programming convenience does not outweigh
performance gains. IOW, I wouldn't write CGI's in assembly because web
servers being normally written in C would probably be the
bottleneck :) (greatly simplifying, of course)

Besides, interpolation is almost always more legible than
concatenation, the fact that rarely goes hand in hand with efficiency.
It would be a sin not to exploit their being identical (not to be
taken literally). Sadly, most strings I've seen are being built up
disregarding interpolation. This struck my curiosity enough to ask
here.
Before anyone gets too concerned about small details like this
(especially when Perl is not constrained to implement them in any way
and the next release of Perl might do it differently), try profiling
the application that you are concerned about. You'll find easier and
slower things to fix and worry about :)

Ok this seems to be coming from the innards of great developing
experience. Again, the reason I asked is based on my strive for
perfection, not that I was counting on an answer here to make more
money or anything.

/Here I reread what's been written, and .../

Still... why shouldn't Perl (or language Foo) programmers care about
efficiency if OS programmers _do_ care?
 
G

Greg Bacon

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

These compile to the same ppcode.

Greg
 
B

Ben Morrow

Quoth rihad said:
Besides, interpolation is almost always more legible than
concatenation,

See also http://blog.plover.com/prs/nyc.html .
Ok this seems to be coming from the innards of great developing
experience. Again, the reason I asked is based on my strive for
perfection, not that I was counting on an answer here to make more
money or anything.

/Here I reread what's been written, and .../

Still... why shouldn't Perl (or language Foo) programmers care about
efficiency if OS programmers _do_ care?

The point is, you should care when, and only when, it matters. Worrying
about micro-optimization without having profiled first is always a waste
of time, and only leads to writing illegible code.

Ben
 
C

Charlton Wilbur

bdf> I like "optimal" still, but people have to realize that it's
bdf> a relative and subjective term based on the assumptions and
bdf> goals of the person using it. It's not meaningless although
bdf> it is often used without context and with the implicit
bdf> asumptions that everyone wants the same thing. :)

I'm not sure it's subjective, and I certainly don't think it's
meaningless; I think in common usage it's vague, and I agree that the
context is almost always incorrectly implied or inferred.

Charlton
 
R

rihad

When you start looking at the speed of things like this and actaully
caring about the answer, you'll probably find that Perl is not the
right tool for the job. Perl is really fast, but if this sort of
operation is too slow for you, then the bigger tasks in Perl, uch as
using a module, will just kill your performance (relatively).

I'm not saying I want Perl to be faster than assembly. I just want to
write fastest Perl code I can, all constrained within Perl's own
performance overhead. Imagine tomorrow there will be (if not already)
some Perl hardware executing opcode natively, and then what, all of a
sudden micro-tunings begin to make sense? In this respect, I don't
believe there's any obvious reason to take the sub-optimal path in any
language (if you have the time).
The point is, you should care when, and only when, it matters. Worrying
about micro-optimization without having profiled first is always a waste
of time, and only leads to writing illegible code.
I couldn't agree more. I'm only micro-optimizing obvious usage
patterns such as string interpolation/concatenation vs. list (such as
print "$foo $bar"; vs. print $foo, ' ', $bar;) which is *always*
faster by its nature. In assembly, one would surely be using "inc
[mem]" instead of "add [mem], 1" because the former is more optimal
[sic].
 
R

rihad

Let me see if I understand correctly. The one that should be used is

print $foo, ' ', $bar;

since printing a list is faster than building a string and printing that.

I don't understand why in your original posting you referred to
"the list variant separated by commas" as being suboptimal.

Damn ;-) I hoped no one would notice. It's the other way around. Read
it as I mean it (c)
 
P

Peter J. Holzer

Quoth Michele Dondi said:
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: [...]
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.

the time difference between

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

and

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

is real on some machines (it is very noticable on my Core2 in 32 bit
mode (19.3 vs. 15.5 seconds) less noticable on a Core2 in 64 bit mode or
a P4, and unmeasurable on a PIII - perl 5.8.8 in all cases), so the code
can't be the same. Either the output of perl -MO=Concise is wrong or at
least incomplete or you are interpreting it wrong (how does it know that
there are instructions which were optimized away? Are the instructions
perhaps still there with a "don't execute" flag? Or does it produce its
output at an earlier stage of compilation?). One aspect which is
obviously missing from the output are the adresses of the instructions.
Alignment can have a large impact on performance.

hp
 
R

rihad

the time difference between

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

and

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

is real on some machines (it is very noticable on my Core2 in 32 bit
mode (19.3 vs. 15.5 seconds) less noticable on a Core2 in 64 bit mode or
a P4, and unmeasurable on a PIII - perl 5.8.8 in all cases), so the code
can't be the same. Either the output of perl -MO=Concise is wrong or at

Try running each test several times and comparing the average times. I
can't see any difference beyond +/- 1% error, can you? Then again, my
Athlon XP might be the "unmeasurable" one.
 
P

Peter J. Holzer

Try running each test several times and comparing the average times.

I did that, of course. The timings are stable to about 0.2 seconds -
much less than the difference. (Hardly surprising for a program which is
almost completely CPU bound).

The difference almost vanishes if the builtin variables $a and $b are
replaced by lexical variables:

#!/usr/bin/perl
use warnings;
use strict;
my ($x, $y);
$x=$y=303; print "$x $y" for 1..39999999;

takes 17.70s, 17.70s, 17.80s user time in three successive runs.

#!/usr/bin/perl
use warnings;
use strict;
my ($x, $y);
$x=$y=303; print $x." ".$y for 1..39999999;

takes 17.57s, 17.44s, 17.59s user time in three successive runs.
I was running the scripts alternately to reduce the influence of other
task potentially running at the same time. The concatenation version
still seems a bit faster, but that could be a measurement error.

Note that the timings are now almost exactly between the timings for the
same scripts with $a and $b - one got faster and the other slower.

Using our gives similar results as my, except that it's about 2 seconds
faster and now the interpolating version is consistently a tiny bit
faster.

So it could be some wierd interaction with $a and $b, although I haven't
the slightest idea what it could be.

(Another unexpected result is that all of these scripts get quite a lot
slower if I use the string "303" instead of the number 303).

hp
 
M

Michele Dondi

Note: crossposted to some supposedly relevant groups. If anyone has
better ones to suggest, then they're welcome.

For people reading this outside of clpmisc, the question arose with
the following post:

<
which in turn was in response to an observation of mine. The whole
thread is available from GG at the following URL:

<http://groups.google.com/[email protected]>


================================================================================


You have a good point; however, as is usual in English grammar,
arguments from ancestry don't always help :). For instance, if 'optimum'
and derived words are necessarily superlative, then 'optimized' means
'made best', and A cannot be more optimized than B either. A has either
been 'made best' or it hasn't.

I'm not sure. 'To optimize' could mean 'to try to reach the optimum'
(or optimal incarnation - of something) thus 'optimized' may mean 'to
have undergone the process of optimization', thus to have gone as much
as possible (wrt some constraints, e.g. time) towards the optimum
without necessarily reaching it. By contrast I see 'optimal' very much
as a synonym of 'optimum' itself, and personally I find much more
acceptable the expression 'more optimzed' than 'more optimal'.
I think what has happened is that, in English, 'optimal' and
'optimized' have acquired something of a sense of 'efficient', which is
clearly comparative, rather than of 'best' in a more general sense. So

That they have been or are occasionally used in that sense may well
be, but I would be surprised to learn that they have actually
"acquired" it. If I paste the remaining two entries found by dict
(which I snipped last time), namely:

: From Moby Thesaurus II by Grady Ward, 1.0 :
:
: 24 Moby Thesaurus words for "optimal":
: best, champion, choice, elect, elite, for the best, greatest,
: handpicked, matchless, optimum, paramount, peerless, picked, prime,
: prize, quintessential, select, supreme, surpassing, unmatchable,
: unmatched, unparalleled, unsurpassed, very best
:
:
:
:
: From The Free On-line Dictionary of Computing (27 SEP 03) :
:
: optimal
:
: 1. Describes a solution to a problem which
: minimises some cost function. Linear programming is one
: technique used to discover the optimal solution to certain
: problems.
:
: 2. Of code: best or most efficient in time,
: space or code size.

you will see that the last one, which is specifically aimed at CS and
IT -and it's actually relevant here- still does not mention just
"efficiency". Thus my take on the issue is that 'optimal' is not just
'efficient' as alleged, but 'the most efficient'. All this, still at a
syntactical level, i.e. we're not discussing yet what "efficient"
could mean.

I'll repeat myself: maybe this strikes me more strongly because of my
implicit Latin heritage, but I still find 'more optimal' to sound like
'more most efficient'.
optimizing a program doesn't necessarily make it better, it simply makes
it more efficient: other things may be more important than efficiency,

This is semantics. "better" and "more efficient" are both
comparatives. That other things may be more important than efficiency
(and indeed I think they are) is irrelevant to the linguistic point
being discussed here.
portability or readability for example. 'Optimum' has not (I would say)

We're not necessarily speaking of computer programs here, and although
it is not in the dictionaries I mentioned before I think that the
italian definition I found for 'ottimale' may well be translated in
English, which is what I'm trying to do now:

: adj: of something that, according to some determinate parameters or
: points of view, represents the *best* possible condition or the *best*
: possible result: e.g. optimal life conditions.

If you accept this, then you can still speak in the context of
programming of a
changed like this, so I find it odd that the dictionaries you quoted say
it is synonymous with 'optimal': I would entirely agree that 'more
optimum' is obviously wrong.

In all earnestness I had never witnessed the use you're reporting of
'optimal', namely that in which it is not a superlative. But
admittedly I do not read *that* much in English.
Theory aside, a quick google shows that 'more optimal' is definitely
acceptable usage; for instance (a random example from the results)

Huh?!? Google may show that "ur so c00l bro" is acceptable usage!!
This is because the claim that A is more optimal or better adapted
than B with respect to some function does not entail that A is
optimal or even good with respect to that function.

http://www.seop.leeds.ac.uk/archives/fall1999/entries/teleology-biology/

which shows that 'optimal' can have the sense of 'efficient' or
'effective' rather than simply 'best'.

Well, that is from an academic institution thus should not fall in the
"ur so c00l bro", but I'm still skeptical: young researchers, however
good may they be in their research field, often tend to speak and
write very bad in their own mother tongue. For example in Italy some
young mathematicians are beginning to use the horrible anglophonic
"surgettiva" in place of the traditional "suriettiva", not that a word
borrowed from English is so bad in and of itself, but it is when
there's a perfectly fine alternative in one's own language.
[English] not only borrows words from other languages; it has on
occasion chased other languages down dark alley-ways, clubbed them
unconscious and rifled their pockets for new vocabulary.
-- James Nicoll

I knew that. In fact it's amongs my .sigs!


Michele
 
V

vocabulary

Note: crossposted to some supposedly relevant groups. If anyone has
better ones to suggest, then they're welcome.

For people reading this outside of clpmisc, the question arose with
the following post:

<
which in turn was in response to an observation of mine. The whole
thread is available from GG at the following URL:

<http://groups.google.com/[email protected]>

===========================================================================­=====

You have a good point; however, as is usual in English grammar,
arguments from ancestry don't always help :). For instance, if 'optimum'
and derived words are necessarily superlative, then 'optimized' means
'made best', and A cannot be more optimized than B either. A has either
been 'made best' or it hasn't.

I'm not sure. 'To optimize' could mean 'to try to reach the optimum'
(or optimal incarnation - of something) thus 'optimized' may mean 'to
have undergone the process of optimization', thus to have gone as much
as possible (wrt some constraints, e.g. time) towards the optimum
without necessarily reaching it. By contrast I see 'optimal' very much
as a synonym of 'optimum' itself, and personally I find much more
acceptable the expression 'more optimzed' than 'more optimal'.
I think what has happened is that, in English, 'optimal' and
'optimized' have acquired something of a sense of 'efficient', which is
clearly comparative, rather than of 'best' in a more general sense. So

That they have been or are occasionally used in that sense may well
be, but I would be surprised to learn that they have actually
"acquired" it. If I paste the remaining two entries found by dict
(which I snipped last time), namely:

: From Moby Thesaurus II by Grady Ward, 1.0 :
:
: 24 Moby Thesaurus words for "optimal":
: best, champion, choice, elect, elite, for the best, greatest,
: handpicked, matchless, optimum, paramount, peerless, picked, prime,
: prize, quintessential, select, supreme, surpassing, unmatchable,
: unmatched, unparalleled, unsurpassed, very best
:
:
:
:
: From The Free On-line Dictionary of Computing (27 SEP 03) :
:
: optimal
:
: 1. Describes a solution to a problem which
: minimises some cost function. Linear programming is one
: technique used to discover the optimal solution to certain
: problems.
:
: 2. Of code: best or most efficient in time,
: space or code size.

you will see that the last one, which is specifically aimed at CS and
IT -and it's actually relevant here- still does not mention just
"efficiency". Thus my take on the issue is that 'optimal' is not just
'efficient' as alleged, but 'the most efficient'. All this, still at a
syntactical level, i.e. we're not discussing yet what "efficient"
could mean.

I'll repeat myself: maybe this strikes me more strongly because of my
implicit Latin heritage, but I still find 'more optimal' to sound like
'more most efficient'.
optimizing a program doesn't necessarily make it better, it simply makes
it more efficient: other things may be more important than efficiency,

This is semantics. "better" and "more efficient" are both
comparatives. That other things may be more important than efficiency
(and indeed I think they are) is irrelevant to the linguistic point
being discussed here.
portability or readability for example. 'Optimum' has not (I would say)

We're not necessarily speaking of computer programs here, and although
it is not in the dictionaries I mentioned before I think that the
italian definition I found for 'ottimale' may well be translated in
English, which is what I'm trying to do now:

: adj: of something that, according to some determinate parameters or
: points of view, represents the *best* possible condition or the *best*
: possible result: e.g. optimal life conditions.

If you accept this, then you can still speak in the context of
programming of a
changed like this, so I find it odd that the dictionaries you quoted say
it is synonymous with 'optimal': I would entirely agree that 'more
optimum' is obviously wrong.

In all earnestness I had never witnessed the use you're reporting of
'optimal', namely that in which it is not a superlative. But
admittedly I do not read *that* much in English.
Theory aside, a quick google shows that 'more optimal' is definitely
acceptable usage; for instance (a random example from the results)

Huh?!? Google may show that "ur so c00l bro" is acceptable usage!!
This is because the claim that A is more optimal or better adapted
than B with respect to some function does not entail that A is
optimal or even good with respect to that function.

which shows that 'optimal' can have the sense of 'efficient' or
'effective' rather than simply 'best'.

Well, that is from an academic institution thus should not fall in the
"ur so c00l bro", but I'm still skeptical: young researchers, however
good may they be in their research field, often tend to speak and
write very bad in their own mother tongue. For example in Italy some
young mathematicians are beginning to use the horrible anglophonic
"surgettiva" in place of the traditional "suriettiva", not that a word
borrowed from English is so bad in and of itself, but it is when
there's a perfectly fine alternative in one's own language.
[English] not only borrows words from other languages; it has on
occasion chased other languages down dark alley-ways, clubbed them
unconscious and rifled their pockets for new vocabulary.
-- James Nicoll

I knew that. In fact it's amongs my .sigs!

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,



Yes, vocabulary in English was made rich by the words from many other
languages. These languages were of the places where British and
English moved in the course of history. I saw a website which helps in
knowing words and building vocabulary. It is www.buildingvocabulary.org.
 
G

GRE

Note: crossposted to some supposedly relevant groups. If anyone has
better ones to suggest, then they're welcome.
For people reading this outside of clpmisc, the question arose with
the following post:

which in turn was in response to an observation of mine. The whole
thread is available from GG at the following URL:
I'm not sure. 'To optimize' could mean 'to try to reach the optimum'
(or optimal incarnation - of something) thus 'optimized' may mean 'to
have undergone the process of optimization', thus to have gone as much
as possible (wrt some constraints, e.g. time) towards the optimum
without necessarily reaching it. By contrast I see 'optimal' very much
as a synonym of 'optimum' itself, and personally I find much more
acceptable the expression 'more optimzed' than 'more optimal'.
That they have been or are occasionally used in that sense may well
be, but I would be surprised to learn that they have actually
"acquired" it. If I paste the remaining two entries found by dict
(which I snipped last time), namely:
: From Moby Thesaurus II by Grady Ward, 1.0 :
:
: 24 Moby Thesaurus words for "optimal":
: best, champion, choice, elect, elite, for the best, greatest,
: handpicked, matchless, optimum, paramount, peerless, picked, prime,
: prize, quintessential, select, supreme, surpassing, unmatchable,
: unmatched, unparalleled, unsurpassed, very best
:
:
:
:
: From The Free On-line Dictionary of Computing (27 SEP 03) :
:
: optimal
:
: 1. Describes a solution to a problem which
: minimises some cost function. Linear programming is one
: technique used to discover the optimal solution to certain
: problems.
:
: 2. Of code: best or most efficient in time,
: space or code size.
you will see that the last one, which is specifically aimed at CS and
IT -and it's actually relevant here- still does not mention just
"efficiency". Thus my take on the issue is that 'optimal' is not just
'efficient' as alleged, but 'the most efficient'. All this, still at a
syntactical level, i.e. we're not discussing yet what "efficient"
could mean.
I'll repeat myself: maybe this strikes me more strongly because of my
implicit Latin heritage, but I still find 'more optimal' to sound like
'more most efficient'.
This is semantics. "better" and "more efficient" are both
comparatives. That other things may be more important than efficiency
(and indeed I think they are) is irrelevant to the linguistic point
being discussed here.
We're not necessarily speaking of computer programs here, and although
it is not in the dictionaries I mentioned before I think that the
italian definition I found for 'ottimale' may well be translated in
English, which is what I'm trying to do now:
: adj: of something that, according to some determinate parameters or
: points of view, represents the *best* possible condition or the *best*
: possible result: e.g. optimal life conditions.
If you accept this, then you can still speak in the context of
programming of a
In all earnestness I had never witnessed the use you're reporting of
'optimal', namely that in which it is not a superlative. But
admittedly I do not read *that* much in English.
Huh?!? Google may show that "ur so c00l bro" is acceptable usage!!
Well, that is from an academic institution thus should not fall in the
"ur so c00l bro", but I'm still skeptical: young researchers, however
good may they be in their research field, often tend to speak and
write very bad in their own mother tongue. For example in Italy some
young mathematicians are beginning to use the horrible anglophonic
"surgettiva" in place of the traditional "suriettiva", not that a word
borrowed from English is so bad in and of itself, but it is when
there's a perfectly fine alternative in one's own language.
[English] not only borrows words from other languages; it has on
occasion chased other languages down dark alley-ways, clubbed them
unconscious and rifled their pockets for new vocabulary.
-- James Nicoll
I knew that. In fact it's amongs my .sigs!
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,

Yes, vocabulary in English was made rich by the words from many other
languages. These languages were of the places where British and
English moved in the course of history. I saw a website which helps in
knowing words and building vocabulary. It iswww.buildingvocabulary.org.- Hide quoted text -

- Show quoted text -


origins of the English words are many and so mastering them may be
little difficult.
www.vocabularycafe.com
www.improvingvocabulary.org

these may help
 
M

Martin Ambuhl

GRE wrote a message of 205 lines, all of which was quotation except:
origins of the English words are many and so mastering them may be
little difficult.
www.vocabularycafe.com
www.improvingvocabulary.org

these may help

Even though you are using the news client of the clueless,
groups.google.com, you can avoid being tarred with that brush if you
learn how to use it. One of the most important is to snip away all but
those sections of the original post except those necessary and germane
to you response. Quoting the whole original post only to add your own
tiny bit to the end is unnecessary and, in fact, rude.
 
T

teachingvocabulary

Even though you are using the news client of the clueless,
groups.google.com, you can avoid being tarred with that brush if you
learn how to use it. One of the most important is to snip away all but
those sections of the original post except those necessary and germane
to you response. Quoting the whole original post only to add your own
tiny bit to the end is unnecessary and, in fact, rude.

Right. though that was not my point.

I wanted to tell about teaching vocabulary. I teach vocabulary to
students and take help of different sites. Some of them like
www.ultimatevocabulary.com
www.vocabularysoftware.net
www.buildingvocabulary.org
www.ultimatespelling.com
www.vocabularybuilder.info
 
M

Mike Lyle

teachingvocabulary said:
Right. though that was not my point.

I wanted to tell about teaching vocabulary. I teach vocabulary to
students and take help of different sites. Some of them like
[...]

We disbelieve your claim. Your English isn't good enough to make you an
effective English teacher: you're just a low-down commercial spammer
breaking the charter of Usenet discussion groups. Anybody reading your
messages will avoid buying the stuff on principle: these things are
generally garbage anyhow.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top