Reputation: 2444
Typically pure python is ~50x slower than native code (C, Fortran) if it consist of tight loop with simple aritmetics. When you use scipy.odeint like described for example in this tutorial you just write functions do integrate in pure python like this:
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]
This function has to be evaluated manytimes, so I would expect that it would make a huge performace bottleneck, considering that the odeint integrator itself is made in FORTRAN/ODPACK
Does it use something to convert the function f(y,t)
from python to native code?
(like f2py, scipy.weave, cython ...) As far as I know, odeint does not need any C/C++ or Fortran compiler, and it does not increase the initialization time of my python script, so probably f2py and scipy.weave is not used.
I'm asking that question, because, maybe, it would be good idea to use the same approach as scipy.integrate.odeint use for acceleration of tight loops in my own code. Using odeint is even more convenient than using f2py or scipy.weave.
Upvotes: 1
Views: 1010
Reputation: 22681
No it doesn't, the odeint code calls your python function without any optimization.
It wraps your function in ode_function
(see here) that in turn calls your python function with call_python_function
. Then the c
function ode_function
will be used by the odepack module.
If keen on and ODE/PDE integrator supporting python
to C
code conversion/speedup, have a look at pydelay
(link). that uses in fact weave
.
Upvotes: 4