-
Notifications
You must be signed in to change notification settings - Fork 0
/
alternative_cython_approaches.py
48 lines (35 loc) · 1.21 KB
/
alternative_cython_approaches.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# import cython
# import math
# This compiles and runs but produces incorrect results, and is about 10x slower than the Rust extension.
# import cython
# import math
# def cpu_intensive(n: cython.ulong, multiplier: cython.ulong) -> cython.double:
# result: cython.double = 0.0
# i: cython.ulong
# for i in range(10_000_000 * multiplier):
# result += math.sqrt(i**3 + i**2 + i * n)
# return result
# I also tried the following, which compiles but produces "math domain" errors at runtime:
# import math
#
# cpdef double cpu_intensive(long long n, long long multiplier):
# cdef double result = 0.0
# cdef long long i
#
# for i in range(10_000_000 * multiplier):
# result += math.sqrt(i**3 + i**2 + i * n)
#
# return result
# And I tried this, which produces a lot of `nan`s:
# cimport cython
# from libc.math cimport sqrt
# @cython.boundscheck(False)
# @cython.wraparound(False)
# cpdef long double cpu_intensive(long long n, long long multiplier):
# cdef long double result = 0.0
# cdef long long i
# cdef long long argument
# for i in range(10_000_000 * multiplier):
# argument = i**3 + i**2 + i * n
# result += sqrt(argument)
# return result