creating/modifying sparse files on linux

Terry Reedy tjreedy at udel.edu
Wed Aug 17 18:00:37 EDT 2005


<draghuram at gmail.com> wrote in message 
news:1124314877.646737.266290 at z14g2000cwz.googlegroups.com...
>
> Thanks for the info on xrange. Writing single char is just to get going
> quickly. I knew that I would have to improve on that. I would like to
> write chunks of 1MB which would require that I have 1MB string to
> write. Is there any simple way of generating this 1MB string

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.  Since strings are 
immutable, you cannot 'append' to them the way you can to lists.

Terry J. Reedy






More information about the Python-list mailing list