On 14.09.2012 14:19, Chris Angelico wrote: > Err, yes, I did mean ** there. The extra multiplications may be > slower, but which is worse? Lots of list additions, or lots of integer > powers? In the absence of clear and compelling evidence, I'd be > inclined to go with the list comp - and what's more, to skip this > function altogether and use the list comp directly where it's needed.
I did some timing with the following versions of the function: def powerlist1(x, n): result=[1] for i in xrange(n-1): result.append(result[-1]*x) return result def powerlist2(x,n): if n==1: return [1] p = powerlist3(x,n-1) p.append(p[-1]*x) return p def powerlist3(x,n): return [x**i for i in xrange(n)] with Python 2.7 you are quite right, i used x=4. Below n=26 powerlist3 is the fastest version, for n>26 powerlist1 is faster, powerlist2 is always slower than both. With Pypy there is a completely different picture, with n<30 powerlist2 is way faster then the other two, but then powerlist1 is again faster for greater n, somehow because now C long int cannot be used any longer. for really big n powerlist3 always takes very much time :) Alex -- http://mail.python.org/mailman/listinfo/python-list