Accelerating For Loop

Chris Rebert clp2 at rebertia.com
Wed Feb 23 08:37:03 CET 2011


2011/2/22 Şansal Birbaş <sansal.birbas at alarko-carrier.com.tr>:
> 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?
<snip>
>    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)

More efficiently and concisely written as:
fiyat = i*X[4]+j*Y[4]+k*Z[4]+m*T[4]+r*W[4]
combo = [i, j, k, m, r, fiyat]
combinations.append(combo)

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list