more efficient?

Z

Zubin Mithra

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
 
I

Irmen de Jong

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
 
L

Lie Ryan

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 +.
 
Z

Zubin Mithra

thank you for your help and support. i`ll keep your advice in mind. :)

cheers!!!
Zubin



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
+.
 
A

Aahz

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.

Generally speaking, I always use join() if I'm iterating and almost never
when not iterating. The performance-killer is concatenation in a tight
loop.
 

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,127
Latest member
CyberDefense
Top