Reputation: 163
I have large loop that contains abs([imaginary]).
It took me a lot of time to complete the program.
I tried multiple other ways to compute abs() such as when C is imaginary, (real(C)^2+imag(C)^2).^.5.
This result same as abs(C).
When I used tic,toc, (real(C)^2+imag(C)^2).^.5 was slightly faster. So I substituted and ran it again.
However profile shows that when I had abs() was much faster.
How can this happen and how can I make abs(C) process faster?
Upvotes: 0
Views: 31
Reputation: 1390
i take it form your comment that you are using large loops, matlab is not that efficent with those, example:
test = randn(10000000,2);
testC = complex(test(:,1),test(:,2));
%%vector
tic
foo = abs(testC);
toc
%%loop
bar = zeros(size(foo));
tic
for i=1:10000000
bar(i) = abs(testC(i));
end
toc
gives you something like
Elapsed time is 0.106635 seconds.
Elapsed time is 0.928885 seconds.
Thats why i would recommend to calculate abs() outside the loop. If replacing the loop in total is no option you can do so only in Parts. For exsample you could use your loop until you got all your complex numbers, end the loop, calc abs() then start a new loop with those results. Also if each iteration of your loop is independat of other iteration results you might want to look in parfor
as an replacement for for
-loops
Upvotes: 1