Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

3
For loop taking ridiculously long time. I am a beginner and I need some help figuring out how to optimise the code.
Post Body

Hello all, I would really appreciate some help on an issue. I can't share the code snippet here as the project is not mine and I am just helping develop a software for a research group. But I will try to explain the best I can. This is the algorithm:

for values of i from 1 to 10^8:

some basic calculations and append the values from those calculations to a list.(lets say list1) This isdone 5 or 6 times for different formulae and different lists.

if(list5[i 1]-list5[i]>0), break out of the loop.

print all the lists after existing the for loop.

Now, I read that the for loops in python are very slow in general and should be avoided. It is better to use map or list comprehensions. But I am not sure I can use them with my code because of the if condition which requires me to break out if the value of i 1th element of the list is greater than ith element. I am unable to figure out how to achieve that using map or list comprehensions.

I also converted the lists to numpy array and used from_future_import division but none of these seem to speed up the code.

Please suggest possible solutions to speed up the code/ get rid of the for loop. Thanks a lot in advance!

edit: fixed a typo.

edit: Added code snippet with changed variable names. There are two for loops but it's the inner one that slows down the whole thing that's why in the post I talked about only the inner loop.

    for k in range(1,10):
        list1 = []
        list2 = []
        list3 = []
        list4 = []
        list5 =[]

        list1.append(a[k])
        list2.append(b[k])
        list3.append(c[k])
        list4.append(0)

        final = 0


        n = list(range(0, (10 ** 8 - 1)))
        time_step = 10 ** (-2)

        for i in n:


             list.append(((beta / 2) * ((a[i] - (b)) ** 2)))
             m=(k1 / (1   ((a[i] - k3) / k2)) ** m1)
             r=(k4 / (1   (a[i] / k5)) ** m2)
             o = k6 / (1   (b[i] / k7)) ** m3

             time_1 = m* (n   o)   (p * q)
             time_2 = r* s  (t * u)

             A = (g - ((c * l) / (s   l))) / time_1
             B = (l_s / ((l_s   l_d) * time_1))
             C = time_2 / ((time_1) * (c   g))
             D = -1 / time_1

             list1.append(i_d[i]   time_step * ((a - list1[i] - list2[i]) / (l_d   l_s)))


            list2.append(list1[i]   time_step * ((list2[i] - list3[k]) / (c   t)))

            list3.append(list2[i]   time_step * ((list1[i] - list2[i]) / (c  d)))

             list4.append(list3[i]   time_step * (A   B*(list1[i]   list2[i])   C*(list3[i] - list1[i])   D * list2[i]))

             final=(final   list2[i] * list3[i] * time_step)

             if (list1[i   1] - list2[i]) < 0:
                 #print('entered loop at i=',i)
                 break

Author
Account Strength
100%
Account Age
4 years
Verified Email
Yes
Verified Flair
No
Total Karma
30,247
Link Karma
5,643
Comment Karma
22,995
Profile updated: 6 hours ago
Posts updated: 1 month ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
3 years ago