Strings in Python (as in Java) are immutable. That means, that when you concatenate 2 strings what you are really doing is creating a new one from the 2 old ones. This can be very inefficient, how much? To the point of, in Jython, talking days just to prepare a String with around 400Kb, with several concats per line.

Solution? Can be found here
.

So, instead of doing str += ‘bla’, do something like str = “”.join([str, ‘bla’]).

I would also add an important tidbit: If you need to do lots of concatenations, append to a list and join at the end, don’t do join over join over join, ie, don’t do:

my_str = ''
for i in range(1000):
  my_str = ''.join([my_str, str(i)])

Do instead:

my_str_list = []
for i in range(1000):
  my_str_list.append(str(i))
my_str = "".join(my_str_list)

If you use the first dialect the result will be as bad as +=. If you use the second, things that took days will take less than a second.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • connotea
  • DZone
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati

Please share your thoughts