Quickest way to build a long string?

Dale Strickland-Clark dale at out-think.NOSPAMco.uk
Thu Oct 12 11:19:10 EDT 2000


Greg Landrum <glandrum at my-deja.com> wrote:

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


That's excellent. Thanks very much. :^)

You wouldn't like to post your code would you? I'm curious to know how
closely your approach is likely to match my own.

Also - what method did you use to profile?

Thanks
Dale Strickland-Clark
Out-Think Ltd
Business Technology Consultants





More information about the Python-list mailing list