reduce vs. for loop
Steven Majewski
sdm7g at virginia.edu
Tue Mar 26 17:24:53 EST 2002
On Tue, 26 Mar 2002, Sean 'Shaleh' Perry wrote:
>
> 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))
> >
>
> bigger problem here. fact2 returns a long, fact1 returns an integer. So
> fact1(100) fails.
>
reduce(operator.mul, range(1,n+1), 1L )
fixes that, but it's even slower.
-- Steve Majewski
More information about the Python-list
mailing list