John Lenton wrote: > On Mon, Feb 07, 2005 at 09:39:11PM +0100, Peter Otten wrote: >> John Lenton wrote: >> > For example, the fastest way >> > to get the factorial of a (small enough) number in pure python is >> > >> > factorial = lambda n: reduce(operator.mul, range(1, n+1)) >> >> I see that you are seduced by the beauty of the expression. Otherwise, if >> you would really care for speed: >> >> [...] > > that's cheating: you moved the calculation into the setup. You aren't > timing what you say you are.
*You* are cheating when you take a predefined function implemented in C (operator.mul) and then claim you are using pure Python. This gives you the extra 50 percent that overcompensate the inefficiency of reduce() -- but only by a hair's breadth: $ python2.4 -m timeit -s'mul = lambda x, y: x * y' -s'a=1' 'mul(a, a)' 1000000 loops, best of 3: 0.522 usec per loop $ python2.4 -m timeit -s'from operator import mul' -s'a=1' 'mul(a, a)' 1000000 loops, best of 3: 0.345 usec per loop Peter -- http://mail.python.org/mailman/listinfo/python-list