通常是纯python is ~50x slower than native code(C,Fortran),如果它由简单的aritmetics紧密循环组成 . 当您在tutorial中使用scipy.odeint之类的描述时,您只需编写函数,就像这样集成在纯python中:
def f(y, t):
Si = y[0]
Zi = y[1]
Ri = y[2]
# the model equations (see Munz et al. 2009)
f0 = P - B*Si*Zi - d*Si
f1 = B*Si*Zi + G*Ri - A*Si*Zi
f2 = d*Si + A*Si*Zi - G*Ri
return [f0, f1, f2]
这个函数必须多次评估,所以我认为它会产生巨大的性能瓶颈,因为odeint集成器本身是在FORTRAN / ODPACK中制作的 .
Does it use something to convert the function f(y,t) from python to native code? (比如f2py,scipy.weave,cython ......)据我所知,odeint不需要任何C / C或Fortran编译器,也不会增加我的python脚本的初始化时间,所以可能是f2py和scipy .weave没用过 .
我问这个问题,因为,也许,使用与scipy.integrate.odeint相同的方法来加速我自己的代码中的紧密循环是个好主意 . 使用odeint比使用f2py或scipy.weave更方便 .
1 回答
不,它没有,odeint代码调用你的python函数没有任何优化 .
它将你的函数包装在
ode_function
(参见here)中,然后用call_python_function
调用你的python函数 . 然后odepack模块将使用c
函数ode_function
.如果热衷于和ODE / PDE集成商支持
python
到C
代码转换/加速,请看pydelay
(link) . 实际使用weave
.