Slow output

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jun 27 16:27:47 EDT 2012


On Wed, 27 Jun 2012 19:06:08 +0100, MRAB wrote:

> On 27/06/2012 18:33, subhabangalore at gmail.com wrote:
>> Dear Group,
>> I am Sri Subhabrata Banerjee writing from India. I am running a small
>> program which exploits around 12 1 to 2 KB .txt files. I am using MS
>> Windows XP Service Pack 3 and Python 2.6 where IDLE is GUI. The text is
>> plain ASCII text. The RAM of the machine is around 2 GB. To run the
>> program the machine is becoming dead slow and it is executing only
>> after several minutes.
[...]
> Could you post the program here (you say that it's small) so that we can
> see what you're trying to do.

I bet that he is building up long strings using repeated string 
concatenation:

s = ''
for item in many_items:
    s += item


instead of accumulating them into a list, then joining them all at once.

Repeated string concatenation can be *painfully* slow, especially under 
Windows. (For some reason, the details of Windows memory management 
sometimes prevents the string concat optimization from working.)


-- 
Steven



More information about the Python-list mailing list