Why do I encounter performance issues while executing FOR-loops with complex numbers?
Show older comments
When I execute the following code:
nd =1600 ;nfc = 800;
M = repmat( diag( fft(eye(nfc)) ).',[nd 1]);
tic % Copy M into F, using a forward loop
F = zeros(nd, nfc);
for kf=1:nfc
F(:,kf) = M(:,kf);
end
toc
The output is
Elapsed time is 0.11 seconds
Now when a reverse loop is used as follows,
tic
F = zeros(nd, nfc);
% Copy M into F, reverse loop
for kf=nfc:-1:1
F(:,kf) = M(:,kf);
end
toc
the output is
Elapsed time is 12.93 seconds
which is more than 100 hundred times the timing obtained in the first case.
Indicating that, the FOR-loop execution time differs from forward loop to reverse loop.However the timings are consistent with real numbers, as shown below:
M = real(M); % transform M into real numbers
tic % Forward loop
F = zeros(nd, nfc);
for kf=1:nfc
F(:,kf) = M(:,kf);
end
toc
gives
Elapsed time is 0.06 seconds
And,
tic % Reverse loop
F = zeros(nd, nfc);
for kf=nfc:-1:1
F(:,kf) = M(:,kf);
end
toc
also gives
Elapsed time is 0.06 seconds
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!