Accelerating For Loop

Şansal Birbaş sansal.birbas at alarko-carrier.com.tr
Wed Feb 23 08:53:23 CET 2011


Nothing changed, Mr. Chris

-----Original Message-----
From: chris at rebertia.com [mailto:chris at rebertia.com] On Behalf Of Chris Rebert
Sent: Wednesday, February 23, 2011 9:37 AM
To: Şansal Birbaş
Cc: python-list at python.org
Subject: Re: Accelerating For Loop

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