Commit ce64b3b6 authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2359 from gabrieldemarmiesse/test_profiling_tutorial_4

Added tests to "profiling tutorial" part 4
parents af951dcf 0d334ef0
# cython: profile=True
# calc_pi.pyx
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
......@@ -213,21 +213,9 @@ so it would be wise to turn it into a cdef to reduce call overhead. We should
also get rid of the power operator: it is turned into a pow(i,2) function call by
Cython, but we could instead just write i*i which could be faster. The
whole function is also a good candidate for inlining. Let's look at the
necessary changes for these ideas::
necessary changes for these ideas:
# encoding: utf-8
# cython: profile=True
# filename: calc_pi.pyx
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
.. literalinclude:: ../../examples/tutorial/profiling_tutorial/calc_pi_3.pyx
Now running the profile script yields:
......
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