, , - - , . ,
:
cdef int numStages = 3
cdef double* coeffs = [0.,0.,1.3]
def RungeKutta_StaticArrayGlobal():
return numStages
cython
:
cdef class RungeKutta_StaticArrayClass:
cdef double* coeffs
cdef int numStages
def __cinit__(self):
self.coeffs = [0.,0.,1.3]
self.numStages = 3
def GetnumStages(self):
return self.numStages
def Integrate(self):
self.coeffs = [0.,0.,0.,0.,0.8,2.1]
, , calloc malloc
:
from libc.stdlib cimport calloc, free
ctypedef struct RKHelper:
int numStages
double* coeffs
def RungeKutta_DynamicArray():
cdef:
RKHelper firstRKMethod
firstRKMethod.numStages = 3
firstRKMethod.coeffs = <double*> calloc(firstRKMethod.numStages,sizeof(double))
firstRKMethod.coeffs[2] = 1.3
free(firstRKMethod.coeffs)
return firstRKMethod.numStages
, , (.. )
In[1]: print(RungeKutta_DynamicArray())
3
In[2]: print(RungeKutta_StaticArray())
3
In[3]: obj = RungeKutta_StaticArrayClass()
In[4]: print(obj.GetnumStages())
3
In[5]: %timeit RungeKutta_DynamicArray()
10000000 loops, best of 3: 65.2 ns per loop
In[6]: %timeit RungeKutta_StaticArray()
10000000 loops, best of 3: 25.2 ns per loop
In[6]: %timeit RungeKutta_StaticArrayClass()
10000000 loops, best of 3: 49.6 ns per loop
RungeKutta_StaticArray no-op, , . coeffs , - . RungeKutta_StaticArrayClass, , , .