# Accelerating For Loop

Şansal Birbaş sansal.birbas at alarko-carrier.com.tr
Wed Feb 23 07:34:43 CET 2011

```Hi All,

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?

Here is the code:

# -*- coding: cp1254 -*-

from numpy import *

# Cihazlar [UI, UO, AO, BO, Fiyat]

M0100=array([10,0,0,0,291])
M0320=array([32,0,0,0,504])
M016160=array([16,0,0,16,604])
ME812U=array([12,8,0,0,817])
SE6104A=array([10,0,4,6,287])
SE6166=array([16,0,6,6,404])
ZN220=array([2,0,0,2,62])
ZN253=array([5,0,3,2,124])
ZN551=array([5,0,1,5,104])
ZN141V=array([4,0,1,1,129])
ZN341V=array([4,0,1,3,136])
MELGR200=array([0,0,0,0,1239])
MELGR25=array([0,0,0,0,575])
ME812uLGR=array([12,8,0,0,1452])
MX0320=array([32,0,0,0,430])
MX16160=array([16,0,0,16,537])
MEX016U=array([16,0,0,0,249])
MEX48U=array([8,4,0,0,210])
MEX88U=array([8,8,0,0,301])
MEX816U=array([16,8,0,0,470])

Xo=ME812U.copy()
Yo=MEX816U.copy()
Zo=MX0320.copy()
To=MX16160.copy()
Wo=MEX88U.copy()

hedef = array([20,15,5,6,0]) # Saha istasyonu için gerekli giriş çıkış sayısı [AI, AO, BI, BO]

def CombiHesapla1(X,Y,Z,istenen,max_expander):

#max_expander Kontrol Cihazı Başına max. Expander sayısı

kalanUO=0
combinations=[]
min_price=999999999
cheapest_combi=[]

for i in range(1,5):
for j in i*range(max_expander+1):
for k in i*range(max_expander+1):

if (j+k)<=(i*max_expander):

mevcut=zeros((1,5))
mevcut+=X*i
mevcut+=Y*j
mevcut+=Z*k
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)
fiyat=i*X[4]+j*Y[4]+k*Z[4]
temp.append(fiyat)
combinations.append(temp)

if len(combinations)>0:
for row in combinations:
if (row[3]<min_price):
min_price=row[3]
cheapest_combi=row

return cheapest_combi

def CombiHesapla2(X,Y,Z,T,W,istenen,max_expander):

#max_expander Kontrol Cihazı Başına max. Expander sayısı

kalanUO=0
combinations=[]
min_price=999999999
cheapest_combi=[]

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)

if len(combinations)>0:
for row in combinations:
if (row[5]<min_price):
min_price=row[5]
cheapest_combi=row

return cheapest_combi

print CombiHesapla1(Xo,Yo,Zo,hedef,5)

print CombiHesapla2(Xo,Yo,Zo,To,Wo,hedef,5)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110223/9441c502/attachment.html>
```