Nearly identical code, hugely different runtimes
Show older comments
Hi all,
I've got a weird situation going on. I have the following code:
if true
for ii = 2:natom
tcheck = 1;
while tcheck ~= 0
tcheck = 0;
lx = rand*lxmax;
ly = rand*lymax;
lz = rand*lzmax;
for jj = 1:ii-1
x12 = lx-x(jj);
y12 = ly-y(jj);
z12 = lz-z(jj);
%
x12 = x12-lxmax* round(x12/lxmax);
y12 = y12-lymax* round(y12/lymax);
z12 = z12-lzmax* round(y12/lzmax);
%
dist = sqrt(x12^2 +y12^2 + z12^2);
if dist <= rmax
tcheck =tcheck +1;
break
end
end
end
x(ii) = lx;
y(ii) = ly;
z(ii) = lz;
ii
endif true
natom=4000 and x,y,z are vectors of length 4000. lxmax, lymax, and lzmax are all equal at 40.5 which is what I have "cubesize" set to in the next code. In general you can assume the parameters controlling the iteration process are the same for both.
I am trying to incorporate this code into another program and rewrote it as such:
if true
for i=2:natoms
while flag ~= 0
flag=0;
xtemp=rand*cubesize;
ytemp=rand*cubesize;
ztemp=rand*cubesize;
for j=1:i-1
xvec=xtemp-store(j,3);
yvec=ytemp-store(j,4);
zvec=ztemp-store(j,5);
xvec = xvec-cubesize* round(xvec/cubesize);
yvec = zvec-cubesize* round(yvec/cubesize);
zvec = zvec-cubesize* round(zvec/cubesize);
dist=sqrt(xvec^2+yvec^2+zvec^2);
if dist <= moldia
flag=1;
break;
end
end
end
store(i,2)=i;
store(i,3)=xtemp;
store(i,4)=ytemp;
store(i,5)=ztemp;
flag=1;
i
end
endif true
This code takes a HUGE amount more time to run. What gives? One speeds its way to i=3500 or so in a matter of seconds. The other craws to i=700 or so and then bogs. I even tried swapping out and storing everything in single vectors first with no effect.
The first code was inside a function, so I tried putting my entire new script inside a dummy function to no effect.
Thanks, Nathan
4 Comments
per isakson
on 29 Apr 2016
Nathan Phelps
on 30 Apr 2016
Nathan Phelps
on 30 Apr 2016
Edited: Nathan Phelps
on 30 Apr 2016
Nathan Phelps
on 3 May 2016
Edited: Nathan Phelps
on 3 May 2016
Accepted Answer
More Answers (1)
Roger Stafford
on 30 Apr 2016
Edited: Roger Stafford
on 30 Apr 2016
0 votes
The progress of your routine will be extremely sensitive to the value you give to ‘rmax’ in the one code or ‘moldia’ in the other. I would hope these are set equal. There will also be a dependence on the initial values in x(1), y(1), and z(1), or in store(1,3:5), and on the values you happen to receive from rand.
I do notice one difference in coding. In the first code you ensure that on the first value with ii = 2 you will definitely go through the while loop, whereas in the second code I see no initial value given to flag to ensure that that happens on the first trip with i = 2. If you happened to start with flag equal to 0, it could have a lasting effect on subsequent processing.
However, as Per states, you should use ‘profile’ to analyze how your code is performing.
An interesting investigation would be to previously store a large number of values from rand in an array and change the two codes to access this array sequentially instead of rand. Either that or use rng to set the same seed in advance each time. In that case if you have set all parameters equal, the results should be identical. If they are not, you have the means of finding out why they are not by checking where store begins to differ from x, y, and z.
Categories
Find more on Marine and Underwater Vehicles in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!