-
Notifications
You must be signed in to change notification settings - Fork 36
/
utils_np.pyx
45 lines (40 loc) · 1.04 KB
/
utils_np.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# distutils: language = c
# cython: language_level=3
# cython: linetrace=True
# cython: infer_types=True
import numpy as np
cimport numpy as np
cimport cython
DTYPE = int
ctypedef np.int_t DTYPE_t
DTYPE8 = np.int8
ctypedef np.int8_t DTYPE8_t
@cython.boundscheck(False)
@cython.wraparound(False)
cpdef idx_to_signal(DTYPE_t[:] idx, int step):
"""Benchmark for 1,000,000 loops (step=250000):
Cython: 18.61 s
Python: 31.30 s
"""
signal = np.zeros((step, 1), dtype=DTYPE8, order='F')
cdef int n = idx.size
cdef DTYPE8_t[::1, :] signal_view = signal
cdef DTYPE_t[:] idx_view = idx
cdef int i
with nogil:
for i in range(n):
signal_view[idx_view[i]][0] = 1
return signal
@cython.boundscheck(False)
@cython.wraparound(False)
cpdef check_zero_signal(DTYPE8_t[:] signal):
"""Benchmark for 1,000,000 loops (1000/2000):
Cython: 1.45 s
Python: 3.67 s
"""
cdef int n = signal.size
cdef int i
for i in range(n):
if signal[i] == 1:
return True
return False