modifying small chunks from long string

Steven D'Aprano steve at REMOVEMEcyber.com.au
Mon Nov 14 03:10:04 EST 2005


Replying to myself... the first sign of madness *wink*


Steven D'Aprano wrote:


> size =  1024*1024*20  # 20 MB
> original = "A" * size
> copy = [None] * size
> for i in range(size):
>     copy[i] = original[i].lower()
> copy = ''.join(copy)

Do you notice the premature optimization? Rather than 
appending new data to an initially empty list, I 
thought I would "optimize" my code by pre-allocating 
all the memory I would need for the list.

You can see how well it didn't work:

> This takes 530 seconds (8 minutes) 

The more sensible algorithm, without the unneeded 
pre-allocation of the copy list, runs 1000 times 
faster. That's the difference between optimization by 
wild guessing and optimization by actually writing good 
code :-)


-- 
Steven.




More information about the Python-list mailing list