File: cython_example.pyx

package info (click to toggle)
python-line-profiler 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,256 kB
  • sloc: python: 8,119; sh: 810; ansic: 297; makefile: 14
file content (26 lines) | stat: -rw-r--r-- 691 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# cython: profile=True
# cython: linetrace=True
# distutils: define_macros=CYTHON_TRACE=1


ctypedef long double Float


def cos(Float x, int n):  # Start: cos
    cdef Float neg_xsq = -x * x
    cdef Float last_term = 1.
    cdef Float result = 0.
    for n in range(2, 2 * n, 2):
        result += last_term
        last_term *= neg_xsq / <Float>(n * (n - 1))
    return result + last_term   # End: cos


cpdef sin(Float x, int n):  # Start: sin
    cdef Float neg_xsq = -x * x
    cdef Float last_term = x
    cdef Float result = 0.
    for n in range(2, 2 * n, 2):
        result += last_term
        last_term *= neg_xsq / <Float>(n * (n + 1))
    return result + last_term  # End: sin