performance of tight loop

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Tue Dec 14 02:05:11 EST 2010


gry wrote:
> I have a little data generator that I'd like to go faster... any
> suggestions?
> maxint is usually 9223372036854775808(max 64bit int), but could
> occasionally be 99.
> width is usually 500 or 1600, rows ~ 5000.
> 
> from random import randint
> 
> def row(i, wd, mx):
>     first = ['%d' % i]
>     rest =  ['%d' % randint(1, mx) for i in range(wd - 1)]
>     return first + rest

A few things here:
 * If you can, don't convert the ints to strings. I'm not 100% sure about
Python 2.4, but newer versions will automatically yield long instead of int
if the range exceeds that of an int, so even with large numbers that should
be safe.
 * Replace range with xrange.
 * Instead of creating and appending lists, you could also use a generator
expression.

>             print ','.join(row(i, width, maxint))

All you do here is take a list of strings, build a single string from them
and then print the string. Why not iterate over the list (or, as suggested,
the generator) and print the elements?

Summary: Avoid unnecessary conversions. This includes int to string, but
also logical sequences into arrays.

Uli

-- 
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932




More information about the Python-list mailing list