how to reduce the time of calling functions
Mostra commenti meno recenti
Risposta accettata
Più risposte (2)
jeewan atwal
il 23 Nov 2019
Modificato: jeewan atwal
il 23 Nov 2019
0 voti
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
Stephen23
il 23 Nov 2019
"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?
jeewan atwal
il 23 Nov 2019
Modificato: per isakson
il 23 Nov 2019
% 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.
@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).
jeewan atwal
il 23 Nov 2019
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.
Stephen23
il 23 Nov 2019
"Could you please share any example where vectorisation was slow compared to for loop."
This commonly occurs when large intermediate arrays are required. You can find examples on this forum, e.g.:
etc.etc.
jeewan atwal
il 23 Nov 2019
0 voti
Thank you. I appreciate your efforts for this valuable info.
Categorie
Scopri di più su Solver Outputs and Iterative Display in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
