more efficient?

Irmen de Jong irmen at -NOSPAM-xs4all.nl
Tue Dec 22 04:30:22 EST 2009


On 12/22/09 7:13 AM, 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!
>
> cheers!!!
> Zubin

1) "premature optimization is the root of all evil."
2) if you're concerned about the speed difference of just a single 
string concat, you shouldn't be using Python.
3) if you're still concerned, use timeit.
4) string concat only gets slow if the number of strings get large. 
Personally I'm only using the join-style-concat if I know the number of 
strings is rather large, or unknown. Otherwise I stick with just the 
regular string concatenation or string formattting (with % or format). 
Much more readable.

-irmen



More information about the Python-list mailing list