Accelerating For Loop
Stefan Behnel
stefan_ml at behnel.de
Wed Feb 23 03:31:04 EST 2011
Şansal Birbaş, 23.02.2011 07:34:
> I needed to find the cheapest combination among given data and I developed an algorithm for this task. It works correctly. But it takes much time (nearly 2 minutes) for second function to find the result while it is just
> one second for the first function. How can I improve the calculation speed?
>[...]
> for i in range(1,5):
> for j in i*range(max_expander+1):
> for k in i*range(max_expander+1):
> for m in i*range(max_expander+1):
> for r in i*range(max_expander+1):
>
> if (j+k+m+r)<=(i*max_expander):
>
> mevcut=zeros((1,5))
> mevcut+=X*i
> mevcut+=Y*j
> mevcut+=Z*k
> mevcut+=T*m
> mevcut+=W*r
> m_UI=mevcut[0][0]
> m_UO=mevcut[0][1]
> m_AO=mevcut[0][2]
> m_BO=mevcut[0][3]
> i_AI=istenen[0]
> i_AO=istenen[1]
> i_BI=istenen[2]
> i_BO=istenen[3]
>
> if (m_UI>=(i_AI+i_BI)):
> if ((m_BO>=i_BO)):
> kalanUO=m_UO
> elif ((m_UO+m_BO)>=i_BO):
> kalanUO=(m_UO+m_BO)-i_BO
> if ((kalanUO+m_AO)>=i_AO):
> temp=[]
> temp.append(i)
> temp.append(j)
> temp.append(k)
> temp.append(m)
> temp.append(r)
> fiyat=i*X[4]+j*Y[4]+k*Z[4]+m*T[4]+r*W[4]
> temp.append(fiyat)
> combinations.append(temp)
For this kind of computation, Cython is usually much faster than NumPy by
itself.
http://cython.org/
http://docs.cython.org/src/tutorial/numpy.html
http://docs.cython.org/src/userguide/numpy_tutorial.html
Stefan
More information about the Python-list
mailing list