-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.pyx
31 lines (26 loc) · 923 Bytes
/
test.pyx
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
#distutils:language =c++
import numpy as np
cimport numpy as np
from libc.math cimport exp, sqrt
from cython.parallel cimport parallel, prange, threadid
import time
cdef complex[:, ::1] compute(size_t n):
cdef double[::1] a = np.linspace(0, 2 * np.pi, n)
cdef complex[:, ::1] m = np.empty((a.size, a.size)).astype(np.complex_)
cdef double k = 100
cdef size_t idx, jdx
cdef size_t N = a.size
for idx in prange(N, nogil = True, num_threads = 16, schedule = 'static'):
for jdx in range(N):
m[idx, jdx] = exp(k * sqrt(a[idx]**2 + a[jdx]**2))
return m
cdef size_t[::1] tests = np.arange(1, 11, dtype = np.uintp) * 1000
cdef double[::1] timings = np.zeros(tests.size)
cdef size_t idx, n
cdef double[::1] a
cdef size_t start_time
for idx in range(tests.size):
start_time = time.time()
compute(tests[idx])
timings[idx] = time.time() - start_time
print(timings.base)