Optimizations (was Re: reduce vs. for loop)
Aahz
aahz at pythoncraft.com
Tue Mar 26 10:05:19 EST 2002
In article <mailman.1017129288.16453.python-list at python.org>,
<m2 at plusseven.com> wrote:
>
>Just out curiosity,
>why is fact2 a bit faster than fact1?
>
>def fact1(n):
> return reduce (operator.mul, range(1L,n + 1))
>
>def fact2(n):
> result = n * 1L
> for i in range(1 , n):
> result *= i
> return result
Try this:
def fact3(n):
mul = operator.mul
return reduce(mul, range(1L, n+1) )
But pay attention to my .sig below.....
--
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
"We should forget about small efficiencies, about 97% of the time.
Premature optimization is the root of all evil." --Knuth
More information about the Python-list
mailing list