0

I have a for loop like

for tot in range(0,100000): 

at first iterations it is quite fast but as it reaches toward the middle it gets extremely slow. I don't have such code lines that you say sth gets accumulated and gets large so it's computation takes longer.

It stays the same as the first iteration always, but gets processed slower.

Is it because each time tot goes from beginning of "range" to reach to the proper iteration, and as iterative method continues it takes longer to reach to middle of the "range"?

I have no idea why this happens!!!

Inside loop sample code:

for tot in range(0,nt): for k in range(0,nx): if k!=0 and k!=nx-1: for i in range(1,nz-1): phai0[i,k]=(1.0/dz**2)*(w0[i+1,k]-2.0*w0[i,k]+w0[i-1,k])+(1.0/dx**2)*(w0[i,k+1]-2.0*w0[i,k]+w0[i,k-1]) phai1[i,k]=(1.0/dz**2)*(w1[i+1,k]-2.0*w1[i,k]+w1[i-1,k])+(1.0/dx**2)*(w1[i,k+1]-2.0*w1[i,k]+w1[i,k-1]) phai2[i,k]=(2.0-Ncoef**2*dt**2)*phai1[i,k]-phai0[i,k]+1.0*(Ncoef**2)*(dt**2*1.0/dz**2)*(w1[i+1,k]-2*w1[i,k]+w1[i-1,k]) if k==0: for i in range(1,nz-1): phai0[i,k]=(1.0/dz**2)*(w0[i+1,k]-2.0*w0[i,k]+w0[i-1,k])+(1.0/dx**2)*(w0[i,k+1]-2.0*w0[i,k]+w0[i,k-2]) phai1[i,k]=(1.0/dz**2)*(w1[i+1,k]-2.0*w1[i,k]+w1[i-1,k])+(1.0/dx**2)*(w1[i,k+1]-2.0*w1[i,k]+w1[i,k-2]) phai2[i,k]=(2.0-Ncoef**2*dt**2)*phai1[i,k]-phai0[i,k]+1.0*(Ncoef**2)*(dt**2*1.0/dz**2)*(w1[i+1,k]-2*w1[i,k]+w1[i-1,k]) if k==nx-1: for i in range(1,nz-1): phai0[i,k]=(1.0/dz**2)*(w0[i+1,k]-2.0*w0[i,k]+w0[i-1,k])+(1.0/dx**2)*(w0[i,1]-2.0*w0[i,k]+w0[i,k-1]) phai1[i,k]=(1.0/dz**2)*(w1[i+1,k]-2.0*w1[i,k]+w1[i-1,k])+(1.0/dx**2)*(w1[i,1]-2.0*w1[i,k]+w1[i,k-1]) phai2[i,k]=(2.0-Ncoef**2*dt**2)*phai1[i,k]-phai0[i,k]+1.0*(Ncoef**2)*(dt**2*1.0/dz**2)*(w1[i+1,k]-2*w1[i,k]+w1[i-1,k]) for N in range(0,N_inner_iteration): sum_max=0 for k in range(0,nx): if k!=0 and k!=nx-1: for i in range(1,nz): if i!=nz-1: w21[i,k]=0.5*(dz**2)*(w20[i,k+1]+w20[i,k-1])/(dx**2+dz**2)+0.5*(dx**2)*(w20[i+1,k]+w20[i-1,k])/(dx**2+dz**2)-(0.5/(dx**2+dz**2))*(dx**2*dz**2)*phai2[i,k] if i==nz-1: w21[i,k]=1/(1+(p(k*dx,1)/(dx*p(k*dx,2)))+((2/p(k*dx,2)*(p(k*dx,1))**2-p(k*dx,0))/dz)+(p(k*dx,1)/dx/dz))*(((p(k*dx,1)/(dx*p(k*dx,2)))+(p(k*dx,1)/dx/dz))*w0[i,k-1]+(((2/p(k*dx,2)*(p(k*dx,1))**2-p(k*dx,0))/dz)+(p(k*dx,1)/dx/dz))*w0[i-1,k]-((p(k*dx,1)/dx/dz))*w0[i-1,k-1]) sum_max=sum_max+abs(w21[i,k]-w20[i,k]) # w20[i,k]=w21[i,k] if k==0: for i in range(1,nz): if i!=nz-1: w21[i,k]=0.5*(dz**2)*(w20[i,1]+w20[i,k-2])/(dx**2+dz**2)+0.5*(dx**2)*(w20[i+1,k]+w20[i-1,k])/(dx**2+dz**2)-(0.5/(dx**2+dz**2))*(dx**2*dz**2)*phai2[i,k] if i==nz-1: k=0.00000001 w21[i,k]=1/(1+(p(k*dx,1)/(dx*p(k*dx,2)))+((2/p(k*dx,2)*(p(k*dx,1))**2-p(k*dx,0))/dz)+(p(k*dx,1)/dx/dz))*(((p(k*dx,1)/(dx*p(k*dx,2)))+(p(k*dx,1)/dx/dz))*w0[i,k-2]+(((2/p(k*dx,2)*(p(k*dx,1))**2-p(k*dx,0))/dz)+(p(k*dx,1)/dx/dz))*w0[i-1,k]-((p(k*dx,1)/dx/dz))*w0[i-1,k-2]) sum_max=sum_max+abs(w21[i,k]-w20[i,k]) if k==nx-1: for i in range(1,nz): # w21[i,k]=w21[i,0] # w20[i,k]=w21[i,k] if i!=nz-1: w21[i,k]=0.5*(dz**2)*(w20[i,1]+w20[i,k-1])/(dx**2+dz**2)+0.5*(dx**2)*(w20[i+1,k]+w20[i-1,k])/(dx**2+dz**2)-(0.5/(dx**2+dz**2))*(dx**2*dz**2)*phai2[i,k] if i==nz-1: w21[i,k]=1/(1+(p(k*dx,1)/(dx*p(k*dx,2)))+((2/p(k*dx,2)*(p(k*dx,1))**2-p(k*dx,0))/dz)+(p(k*dx,1)/dx/dz))*(((p(k*dx,1)/(dx*p(k*dx,2)))+(p(k*dx,1)/dx/dz))*w0[i,k-1]+(((2/p(k*dx,2)*(p(k*dx,1))**2-p(k*dx,0))/dz)+(p(k*dx,1)/dx/dz))*w0[i-1,k]-((p(k*dx,1)/dx/dz))*w0[i-1,k-1]) sum_max=sum_max+abs(w21[i,k]-w20[i,k]) # w20[i,k]=w21[i,k] w20[:]=w21[:] if (1.0/(nx*nz))*sum_max<0.0000001: break print (1.0/(nx*nz))*sum_max, "N=",N w0[:]=w1[:] w1[:]=w20[:] w_final[:,:,tot]=w1[:] for ordstep in range(1,num_of_orders+1): integ_sum2=0 for zstep in range (0,nz): for xstep in range (0,nx): integ_sum2=integ_sum2+w1[zstep,xstep]*np.sin(ordstep*(k_z*1.0/m)*zstep*dz)*np.sin(ordstep*(k_x*1.0/n)*xstep*dx-omega*tot*dt)*dx*dz Amp[ordstep-1,tot]=4.0/(l*h)*integ_sum2/Ampref if tot%1==0: Ampsave=np.reshape(Amp[:,tot],(1,5)) with open('test.csv', 'a') as file: np.savetxt(file,np.array(Ampsave)) # np.save(outfile,Amp) oldcol = wframe wframe = ax.plot_surface(X, Z, w1, rstride=2, cstride=2) if oldcol is not None: ax.collections.remove(oldcol) plt.pause(.001) ax.set_xlabel('(X)') ax.set_ylabel('(Z)') ax.set_zlabel('$ W_{Numerical} $') plt.figure('Amplitude Evolution') plt.axis([0, 40000, -2, 2]) plt.scatter(tot, Amp[0,tot]) plt.scatter(tot, Amp[1,tot]) print Amp[0,tot] plt.draw() plt.legend(bbox_to_anchor=(1, 1), loc=1, borderaxespad=0.) plt.title('Amplitude Evolution') plt.xlabel('Time[s]',fontsize=25) plt.ylabel('Amplitude',fontsize=25) plt.savefig("res.png", transparent = True, pad_inches=0) plt.show() 

I have also changed all range to xrange and avoided using np. inside the loops by predefining them.

6
  • 2
    It would be helpful if you posted your code that's running inside the loop, as it is likely the culprit here. Commented Mar 29, 2016 at 17:27
  • @iMassakre added the code Commented Mar 29, 2016 at 17:34
  • Where does the value of N_inner_iteration come from? Does it ever change while the loop executes? Commented Mar 29, 2016 at 17:48
  • no no it is a fixed value Commented Mar 29, 2016 at 17:58
  • Loops that slow down as the iteration count increases are a classic symptom of Schlemiel the Painter-style algorithms. Commented Mar 29, 2016 at 18:08

1 Answer 1

1

Are you using Python2.7 ou Python3.x?

If you are using Python2.7 you should choose xrange generator:

for tot in xrange(0,100000): print tot 

It will be faster for sequences large than this. :-)

Sign up to request clarification or add additional context in comments.

1 Comment

I am using Pycharm 4.0.4

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.