reduce vs. for loop

Sean 'Shaleh' Perry shalehperry at attbi.com
Tue Mar 26 11:07:09 EST 2002


On 26-Mar-2002 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
> 

bigger problem here.  fact2 returns a long, fact1 returns an integer.  So
fact1(100) fails.




More information about the Python-list mailing list