MATLAB suddenly behaving very slowly...?

1 view (last 30 days)
Anish
Anish on 11 Apr 2014
Edited: Image Analyst on 12 Apr 2014
Hi!
I was working on some code to barrel distort an image, then I saved the code, took a lunch break, and when I came back (not making any changes whatsoever), the code was running orders of magnitudes slower.
After doing some playing around with the tic and toc functions, I have obtained the following very strange result
(I have tried restarting my computer/MATLAB several times already):
%I, m2, n2, and A are all m x n x 1 arrays
%i=j=1
tic;
I(m2(i, j), n2(i, j)) = A(i, j);
toc;
Elapsed time is 0.000003 seconds.
tic;
for i = 1:1
for j = 1:1
I(m2(i, j), n2(i, j)) = A(i, j);
end
end
toc;
Elapsed time is 0.001875 seconds.
(This code was run in a script, so there's no delay from not pressing enter or something like that).
What do you make of this? This is a 625-fold increase in time!
My first reaction was there's some overhead in calling the 2 for loops, however this is not the case, as I can demonstrate with the following example:
tic;
A(1, 1) = 1;
toc;
Elapsed time is 0.000002 seconds.
tic;
for i = 1:1
for j = 1:1
A(1, 1) = 1;
end
end
toc;
Elapsed time is 0.000001 seconds.
It's quite baffling to me...
  2 Comments
Walter Roberson
Walter Roberson on 11 Apr 2014
In the first case, i and j are going to have their default values as being sqrt(-1)
Anish
Anish on 11 Apr 2014
To clarify - i and j had been set to 1 (i = 1; j = 1;) before that.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!