how to reduce the time of calling functions

 Risposta accettata

Walter Roberson
Walter Roberson il 23 Nov 2019
2/3 of the time is spent executing obj() which is not a Mathworks supplied function. You should investigate obj() to see how it could be sped up.
1/3 of the time is spent executing squeeze(), which is a Mathworks supplied function. As you are calling it many times in a loop, chances are good that you know which dimensions are expected to be singular, and in that situation there is a fairly strong chance that you can replace the call to squeeze() with a call to reshape() which would be more efficient because it would not need to do tests on the dimensions. The savings are probably not all that high though.

Più risposte (2)

jeewan atwal
jeewan atwal il 23 Nov 2019
Modificato: jeewan atwal il 23 Nov 2019
If you are callng MATLAB default functions, then, you cant do much as MATLAB default functions are very well optimized.
However, If you have created some funtions by yourself, then you need to optimize those functions to speed up the things.
Large For loops make MATLAB scripts slow.
If you have large For loop in the functions scripts, then to avoid those For loops you can use MATLAB vectorisation techinques to speed up things.
Hope this helps

5 Commenti

"Large For loops make MATLAB scripts slow."
Really? Can you show any examples that support this idea?
"If you have large For loop in the functions scripts, then to avoid those For loops you can use MATLAB vectorisation techinques to speed up things."
Really? Is vectorized code always faster than loops?
% Loops:
A = rand(1500);
B = rand(1500);
C = zeros(size(A));
tic
for col = 1:size(A, 2) % Columns are in the outer loop
for row = 1:size(A, 1) % Rows in the inner loop
C(row, col) = A(row, col) + B(row, col);
end
end
toc
% Vectorized:
C = A + B;
toc
Elapsed time is 1.713167 seconds.
Elapsed time is 0.004025 seconds.
Stephen23
Stephen23 il 23 Nov 2019
Modificato: Stephen23 il 23 Nov 2019
@jeewan atwal : sure, it is easy to find some examples where vectorized code is faster than a loop or two, but that is not what I asked about. You wrote that vectorized code is always faster than for loops "Large For loops make MATLAB scripts slow. ... to avoid those For loops you can use MATLAB vectorisation techinques to speed up things". You did not write that vectorization is sometimes faster, or under which circumstances vectorization can be worthwhile, you wrote that vectorized code is faster than using "large" for loops.
What I asked is "Is vectorized code always faster than loops?" (hint: the answer is no).
I am not claiming that it is always faster. But at least in my problems, i found it faster than for loop. Could you please share any example where vectorisation was slow compared to for loop.

Accedi per commentare.

jeewan atwal
jeewan atwal il 23 Nov 2019
Thank you. I appreciate your efforts for this valuable info.

Community Treasure Hunt

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

Start Hunting!

Translated by