Just wrote a code for comparison of speed of calculation for a function which is in written as numpy
and a function which uses ufuncify
from sympy
:
刚刚编写了一个代码,用于比较函数的计算速度,该函数写为numpy,函数使用来自sympy的ufuncify:
import numpy as np
from sympy import symbols, Matrix
from sympy.utilities.autowrap import ufuncify
u,v,e,a1,a0 = symbols('u v e a1 a0')
dudt = u-u**3-v
dvdt = e*(u-a1*v-a0)
p = {'a1':0.5,'a0':1.5,'e':0.1}
eqs = Matrix([dudt,dvdt])
numeqs=eqs.subs([(a1,p['a1']),(a0,p['a0']),(e,p['e'])])
print eqs
print numeqs
dudt = ufuncify([u,v],numeqs[0])
dvdt = ufuncify([u,v],numeqs[1])
def syrhs(u,v):
return dudt(u,v),dvdt(u,v)
def nprhs(u,v,p):
dudt = u-u**3-v
dvdt = p['e']*(u-p['a1']*v-p['a0'])
return dudt,dvdt
def compare(n=10000):
import time
timer_np=0
timer_sy=0
error = np.zeros(n)
for i in range(n):
u=np.random.random((128,128))
v=np.random.random((128,128))
start_time=time.time()
npcalc=np.ravel(nprhs(u,v,p))
mid_time=time.time()
sycalc=np.ravel(syrhs(u,v))
end_time=time.time()
timer_np+=(mid_time-start_time)
timer_sy+=(end_time-mid_time)
error[i]=np.max(np.abs(npcalc-sycalc))
print "Max difference is ",np.max(error), ", and mean difference is ",np.mean(error)
print "Average speed for numpy ", timer_np/float(n)
print "Average speed for sympy ", timer_sy/float(n)
On my machine the result is:
在我的机器上,结果是:
In [21]: compare()
Max difference is 5.55111512313e-17 , and mean difference is 5.55111512313e-17
Average speed for numpy 0.00128133814335
Average speed for sympy 0.00127074036598
Any suggestions on how to make either of the above function faster is welcome!
关于如何使上述任一功能更快的任何建议都是受欢迎的!
1 个解决方案
#1
0
After further exploration it seems that ufuncify
and regular numpy
functions will give more or less the same speed of computation. Using numba
or printing to theano
function did not result in a faster code. So the other option to make things faster is either cython
or wrapping a c
or FORTRAN
code.
在进一步探索之后,似乎ufuncify和常规numpy函数将提供或多或少相同的计算速度。使用numba或打印到theano函数不会导致更快的代码。因此,提高速度的另一个选择是cython或包装c或FORTRAN代码。
#1
0
After further exploration it seems that ufuncify
and regular numpy
functions will give more or less the same speed of computation. Using numba
or printing to theano
function did not result in a faster code. So the other option to make things faster is either cython
or wrapping a c
or FORTRAN
code.
在进一步探索之后,似乎ufuncify和常规numpy函数将提供或多或少相同的计算速度。使用numba或打印到theano函数不会导致更快的代码。因此,提高速度的另一个选择是cython或包装c或FORTRAN代码。