After a certain time for loop speed slow down?
8 views (last 30 days)
Hi, I am creating a program with a for loop. As I have asked in another question I have learned to speed this process with vector and ok. But I would like to know why this thing happen.
Take this code:
A = zeros(1000,1000);
for k = 1:1000
for j = 1:1000
A(k,j)=E0*exp(-65*((k-500)^2+(j-500)^2)) *exp(-1i *6.5*((k-500)^2+(j-500)^2)); %gaussiana
On my computer the first 500 k are really fast, then after about 500, it is very slow, almost 0.8 sec for one k. Why?
Asad (Mehrzad) Khoddam on 6 Oct 2020
You can create A without having two nested loops:
[km, jm] = meshgrid(1:1000, 1:1000);
A= E0*exp(-65*((km - 500).^2 + (jm - 500).^2)) .*exp(-1i *6.5*((km - 500).^2 + (jm - 500).^2));