I have an array with 40000 numbers that are floats or ints. I need to perform some calculation.
To do this I have used nested for loop, but the code is really slow. Can I use something instead of nested the for loop? Is there any other way to reduce the execution time?
When I changed to a list comprehension the execution time reduce slightly. Why did the list comprehension take less time?
import numpy as np import time as t pox1= np.random.randint(1000, size= 40000) time = np.arange(40000) y=np.zeros(len(pox1)) w=np.zeros(len(pox1)) start = t.time() for num in range (len(time)-1): z= [((pox1[i] - pox1[i-num]) ** 2) for i in range(num, len(pox1))] k=np.mean(z) y[num]=k # for i in range(num, len(pox1)): # z.append((pox1[i] - pox1[i-num]) ** 2) endtime = (t.time()-start) print(y,endtime)