Quickest way to build a long string?

Greg Landrum glandrum at my-deja.com
Thu Oct 12 10:38:03 EDT 2000


In article <u3abuskq55oiugpuafh3fqdkl45s7am6vh at 4ax.com>,
  dale at out-think.NOSPAM.co.uk wrote:
> I'm building a web page (table) from a whole bunch a database fields.
> Each field needs the table row and cell tags added around it and then
> be appended to the results so far.
>
> String operations of this type tend to be memory thrashers so I was
> wondering if anyone had conducted any tests to see which of the
> approaches available might be the most efficient.
>
> 1. multiple string concetenation. (html = html + cell)
> 2. build list of components then use join at the end to make a string
> 3. something even cleverer
>

Well, I don't have anything cleverer, but a short time with the profiler
answered the question to my satisfaction.

On my machine, adding 100 character-long strings 1000 times takes:
1)0.42 seconds
2)0.007 seconds

adding 100 character-long strings 5000 times takes:
1) 12.24 seconds
2) 0.03 seconds

adding 10000 character-long strings 1000 times takes:
1)47.8 seconds
2)0.4 seconds


(if you happen to know how many lines you are going to add, or you
at least have an upper bound, you can speed up method 2 even more
by 'preallocating' the list and assigning rather than appending)

YMMV, but even if it varies a lot I think the choice is clear.
:-)

-greg


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list