[Tutor] Standard Library Performance (3.4.1)
Adam Jensen
hanzer at riseup.net
Fri Oct 24 03:45:48 CEST 2014
I'm tinkering this evening and I've noticed that math.factorial() is
much faster than my plain python implementations.
--------------------------------------------
import math
def factorial(n):
temp = 1
for k in range(0,n):
temp = temp * (n - k)
return(temp)
def fac(n):
return 1 if (n == 0) else n * fac(n-1)
--------------------------------------------
>From IPython:
In [21]: %timeit factorial(9)
100000 loops, best of 3: 5.31 µs per loop
In [22]: %timeit fac(9)
100000 loops, best of 3: 6.86 µs per loop
In [23]: %timeit math.factorial(9)
1000000 loops, best of 3: 416 ns per loop
--------------------------------------------
Is this kind of performance difference typical of the standard library
functions (compared to plain python user implementations)?
More information about the Tutor
mailing list