I can't figure out why among the following loop the f?c
are 20 times slower than the f?
.
I understand type definition allows Cython to leverage C speed.
What am I missing here?
Thanks
%%cython
import numpy as np
cimport numpy as np
cpdef f1(l):
for k in l:
k
cpdef f1c(np.ndarray npl):
cdef int i = 0
for i in range(npl.shape[0]):
npl[i]
cpdef f2(n):
for k in n:
k
cpdef f2c(np.ndarray npn):
cdef int i = 0
for i in range(npn.shape[0]):
npn[i]
and the timings:
l = ["lol"]*100000
npl = np.array(l, dtype=np.str)
n = [1]*100000
npn = np.array(n, dtype=np.int)
%timeit f1(l)
%timeit f1c(npl)
%timeit f2(n)
%timeit f2c(npn)
1000 loops, best of 3: 484 µs per loop
100 loops, best of 3: 13.1 ms per loop
1000 loops, best of 3: 483 µs per loop
100 loops, best of 3: 12.4 ms per loop
The loop over the numpy
is at least one order of magnitude faster when you specify the data type and the number of dimensions of the array:
def f2c(np.ndarray[np.int_t, ndim=1] npn):
cdef int i = 0
for i in range(npn.shape[0]):
npn[i]
similarly, I got a two times faster loop for the string case:
def f1c(np.ndarray[object, ndim=1] npl):
cdef int i = 0
for i in range(npl.shape[0]):
npl[i]
where in this case you must use:
npl = np.array(l, dtype=object)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.