creating/modifying sparse files on linux

Benji York benji at benjiyork.com
Thu Aug 18 08:21:42 EDT 2005


Terry Reedy wrote:
> megastring = 1000000*'a' # t < 1 sec on my machine

>>(other than keep appending to a string until it reaches 1MB len)?
> 
> You mean like (unexecuted)
> s = ''
> for i in xrange(1000000): s += 'a' #?
> 
> This will allocate, copy, and  deallocate 1000000 successively longer 
> temporary strings and is a noticeable O(n**2) operation.

Not exactly.  CPython 2.4 added an optimization of "+=" for strings. 
The for loop above takes about 1 second do execute on my machine.  You 
are correct in that it will take *much* longer on 2.3.
--
Benji York




More information about the Python-list mailing list