* concatenation works only with strings and strings. You can't do:
>>> "Hello. The number of apples I have is: " + 12
But you can do:
>>> "Hello. The number of apples I have is: %d" % (12,)
If all you are doing is a simple concatenation, I don't see any reason
to really worry about the speed, unless you are trying to do megabytes
of string concatenation.
If you use string formatting in other places in your code, yes using it
for simple concatenations would probably enhance consistency of coding
style, but I wouldn't consider it wrong to throw in a concatenation
operator.
You got me curious so I wrote a simple loop to concatenate a very small
string a million times. I ran it using both methods.
jefferya@pax:~$ time python concattest.py
real 0m0.557s
user 0m0.530s
sys 0m0.022s
jefferya@pax:~$ time python concattest2.py
real 0m0.834s
user 0m0.803s
sys 0m0.024s
concattest.py uses the concatenation operator.
concattest2.py uses string formatting.
I would conclude that for the sake of optimization, it is better to use
the concatenation operator for simple cases. I haven't done further
testing to see if string formatting ever performs better in certain
situations, but for the very simple test case I used, string formatting
appears to be slower.
Hopefully this answers your question!
Jeff Anderson
concattest.py:
s1 = "Hello"
i=0
while i<999000:
greeting="English: " + s1
i=i+1
concattest2.py:
s1 = "Hello"
i=0
while i<999000:
greeting="English: %s" % (s1,)
i=i+1
[1] http://diveintopython.org/native_data_types/formatting_strings.html
I had thought that I read from that same source that formatting is
faster than concatenation (and that's why I checked there before
responding), but I don't see that mentioned. Not sure where I got that
idea or if its even true.
[1]: http://diveintopython.org/native_data_types/formatting_strings.html
--
----
Waylan Limberg
way...@gmail.com
Formatting is faster than serial concatenation, which often results in
copying the same substring multiple times. A single concatenation is
usually faster than formatting, however.
--
Best regards, Yuri V. Baburov, ICQ# 99934676, Skype: yuri.baburov,
MSN: bu...@live.com
PEP 8 has something to say about this under "Programming Recommendations":
- Code should be written in a way that does not disadvantage other
implementations of Python (PyPy, Jython, IronPython, Pyrex, Psyco,
and such).
For example, do not rely on CPython's efficient implementation of
in-place string concatenation for statements in the form a+=b or a=a+b.
Those statements run more slowly in Jython. In performance sensitive
parts of the library, the ''.join() form should be used instead. This
will ensure that concatenation occurs in linear time across various
implementations.
Of course, this doesn't say anything about formatting - just joins,
but concatenation seems to be generally discouraged wherever we look.
It's also discussed in Recipe 3.5, "Combining Strings", in the Python Cookbook:
Ronny
that's outdated information -- recent Python versions (2.4 and later)
can do "+" and "+=" in place in many cases. join is still preferred if
you have lots of string fragments, but using it for 2-3 pieces is
usually pointless.
</F>