herzog at online.de
Tue Apr 18 03:19:01 CEST 2000
Michael Hudson <mwh21 at cam.ac.uk> writes:
> For that last dreg of performance you might try:
> import random
> def do_it(randint=random.randint,xrange=xrange):
> r = [0,] * 100000
> for i in xrange(100000):
> r[i] = randint(1,10)
> But really, this is getting silly; if performance is this much of an
> obsession you aren't going to have that much fun with Python.
A much better optimization would be to use choice with a constant list
instead of randint. The implementation of choice is much more efficient
from random import choice
r = [0,] * length
ints = range(1, 11)
for i in xrange(length):
r[i] = choice(ints)
even better would be to inline choice or a custom version of randint.
Of course, this takes us away from the question originally asked...
Bernhard Herzog | Sketch, a drawing program for Unix
herzog at online.de | http://sketch.sourceforge.net/
More information about the Python-list