Anagram

Michael Hudson mwh at python.net
Wed Jan 23 11:42:09 EST 2002


"Alex Martelli" <aleax at aleax.it> writes:

> ...except maybe when you need it to be *REALLY, REALLY FAST*:
> 
> import operator, gmpy, time
> 
> def facop(n):
>     return reduce(operator.mul,range(2,n+1),1L)
[...]
> def facsi(n):
>     result = 1L
>     for i in range(2, n+1):
>         result *= i
>     return result
[...]
> I'm not sure why facop is marginally slower than facsi (?)

I'd imagine it's because each multiplication in facop has to go
through the full call machinery, whereas in facsi it doesn't.

    operator.mul(a, b)

is slower than

    a * b

-- more function calls inside the interpreter.

> ... if your program's bottleneck is a lot of computation
> of factorials of large numbers, it might matter:-).

In this case I think you probably have problems all your own...

Cheers,
M.

-- 
  Need to Know is usually an interesting UK digest of things that
  happened last week or might happen next week. [...] This week,
  nothing happened, and we don't care.
                          -- NTK Know, 2000-12-29, http://www.ntk.net/



More information about the Python-list mailing list