more efficient?

Lie Ryan lie.1296 at gmail.com
Tue Dec 22 09:37:20 EST 2009


On 12/22/2009 5:13 PM, Zubin Mithra wrote:
> I have the following two implementation techniques in mind.
>
> def myfunc(mystring):
>      check = "hello, there " + mystring + "!!!"
>      print check
>
>
> OR
> structure = ["hello, there",,"!!!"]
> def myfunc(mystring):
>      structure[2] = mystring
>      output = ''.join(mystring)
>
> i heard that string concatenation is very slow in python; so should i
> go for the second approach? could someone tell me why? Would there be
> another 'best-practice-style'?
> Please help. Thankx in advance!

Python's strings are immutable and to concatenate two string the 
interpreter need to copy two whole string into a new string object. This 
isn't a performance problem until you're trying to concatenate a list 
containing a thousand strings:
['abc', 'bcd', 'cde', 'def', ...]
with the naive approach:
joined = ''
for s in lst:
     joined = joined + s

first python will conc. '' and 'abc', copying 0+3 = 3 chars
then it conc. 'abc' and 'bcd', copying 3+3 = 6 chars
then it conc. 'abcbcd' and 'cde' copying 6+3 = 9 chars
then it conc. 'abcbcdcde' and 'def' copying 9+3 = 12 chars
and so on...

for four 3-letter strings, python copies 3 + 6 + 9 + 12 = 30 chars, 
instead of the minimum necessary 12 chars. It gets worse as the number 
of strings increases.

When you concatenate two 1000-chars large strings, both + and ''.join 
will have to copy 2000 chars. But when you join one thousand 2-chars 
string you'll need to copy 1001000 chars[!] with +.

Now, early optimization *is evil*. Don't start throwing ''.join every 
here and there. The performance by the concatenations won't start to 
matter until you're concatenating a large lists (>40) and + is much more 
readable than ''.join().

When concatenating small number of strings I preferred 
%-interpolation/str.format; it's often much more readable than ''.join 
and +.



More information about the Python-list mailing list