作者:WenFJ | 来源:互联网 | 2023-09-17 13:25
ChangingcertainCythonvariablesfromtypeinttotypesize_tcansignificantlyreducesomefunctio
Changing certain Cython variables from type int
to type size_t
can significantly reduce some functions times (~30%), but I do not understand why.
将某些Cython变量从int类型更改为type_t类型可以显着减少一些函数时间(~30%),但我不明白为什么。
For example:
例如:
cimport numpy as cnp
import numpy as np
def sum_int(cnp.int64_t[::1] A):
cdef unsigned long s = 0
cdef int k
for k in xrange(A.shape[0]):
s += A[k]
return s
def sum_size_t(cnp.int64_t[::1] A):
cdef unsigned long s = 0
cdef size_t k
for k in xrange(A.shape[0]):
s += A[k]
return s
a = np.array(range(1000000))
And the timing results:
时间结果如下:
In [17]: %timeit sum_int(a)
1000 loops, best of 3: 652 µs per loop
In [18]: %timeit sum_size_t(a)
1000 loops, best of 3: 427 µs per loop
I am new to Cython, and know Fortran better than C. Help me out. What is the important difference between these two variable types that causes such a performance difference? What is it that I don't grok about Cython?
我是Cython的新手,比C.更了解Fortran。帮助我。这两种变量类型之间的重要区别是什么导致了这种性能差异?什么是我不喜欢Cython?
2 个解决方案