Commit ad096ace authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Moved a code snippet from profiling tutorial to the examples directory for testing.

parent 3d291a58
# cython: profile=True
# calc_pi.pyx
cimport cython
@cython.profile(False)
cdef inline double recip_square(int i):
return 1. / (i * i)
def approx_pi(int n=10000000):
cdef double val = 0.
cdef int k
for k in range(1, n + 1):
val += recip_square(k)
return (6 * val) ** .5
...@@ -276,24 +276,9 @@ That bought us another 1.8 seconds. Not the dramatic change we could have ...@@ -276,24 +276,9 @@ That bought us another 1.8 seconds. Not the dramatic change we could have
expected. And why is recip_square still in this table; it is supposed to be expected. And why is recip_square still in this table; it is supposed to be
inlined, isn't it? The reason for this is that Cython still generates profiling code inlined, isn't it? The reason for this is that Cython still generates profiling code
even if the function call is eliminated. Let's tell it to not even if the function call is eliminated. Let's tell it to not
profile recip_square any more; we couldn't get the function to be much faster anyway:: profile recip_square any more; we couldn't get the function to be much faster anyway:
# encoding: utf-8 .. literalinclude:: ../../examples/tutorial/profiling_tutorial/calc_pi_4.pyx
# cython: profile=True
# filename: calc_pi.pyx
cimport cython
@cython.profile(False)
cdef inline double recip_square(int i):
return 1./(i*i)
def approx_pi(int n=10000000):
cdef double val = 0.
cdef int k
for k in xrange(1,n+1):
val += recip_square(k)
return (6 * val)**.5
Running this shows an interesting result: Running this shows an interesting result:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment