get-a-cup-of-coffee slow

Tim Peters tim.one at home.com
Fri Aug 10 07:56:33 CEST 2001


[Grant Griffin]
> I tried the following today:
> 
>    def gen_sectors1():
>        print 'Generating (Version 1)...',
>        s = '' 
>        for i in xrange(65536):
>            s += chr(i & 0xFF)
>        for i in xrange(65536):
>            s += chr((65535 - i) & 0xFF);
>        print 'done'
>        return s

You should be much happier with this:

def gen_sectors2():
    import array
    print 'Generating (Version 2)...',
    s = array.array('c')
    for i in xrange(65536):
        s.append(chr(i & 0xFF))
    for i in xrange(65536):
        s.append(chr((65535 - i) & 0xFF))
    print 'done'
    return s.tostring()

character-arrays-can-be-mutated-in-place-ly y'rs  - tim





More information about the Python-list mailing list