performance of tight loop
Paul Rubin
no.email at nospam.invalid
Tue Dec 14 04:02:06 EST 2010
gry <georgeryoung at gmail.com> writes:
...
> rest = ['%d' % randint(1, mx) for i in range(wd - 1)]
> for i in range(i,i+rows): ...
One thing that immediately comes to mind is use xrange instead of range.
Also, instead of
first = ['%d' % i]
rest = ['%d' % randint(1, mx) for i in range(wd - 1)]
return first + rest
you might save some copying with:
rest = ['%d' % randint(1, mx) for i in xrange(wd)]
rest[0] = '%d'%i
return rest
That's uglier than the old-fashioned
rest = ['%d'%i]
for i in xrange(wd-1):
rest.append('%d' % randint(1, mx))
I think a generator would be cleanest, but maybe slowest:
def row(i, wd, mx):
yield '%d' % i
for j in xrange(wd-1):
yield ('%d' % randint(1, mx))
More information about the Python-list
mailing list