Matlab hidden optimization at the fifth execution

1 view (last 30 days)
I was trying to evaluate how long it takes to compute some equations, so I could estimate how long it will take to process a big amount of data.
I noticed that the 5th time an equation is executed, it takes longer (Matlab is doing something extra) and from that moment, it takes much less computing time than the initial calculations.
Is there any way to configure that hidden optimization? Can it be done at the 2nd execution?
for i=1:10
tic
% Before executing again, change something on the calculation
% to clear the internal optimization
atan2(sin(1/i),cos(i^2));
t(i)=toc;
end
bar(t)

Accepted Answer

Bruno Luong
Bruno Luong on 19 Jul 2022
Edited: Bruno Luong on 19 Jul 2022
I don't think so. I believe it is related to JIT optimization Executio Engine (EE), some code are not "compiled" at the first few iterations, but then when MATLAB detects the same code is performed few times it decides to "compile" it to perform faster. All those details are not documented so it is unlikely for us to be able/know how to change it.
Something that might affected is you should do the benchmark inside a function and not on script. I believe the EE might behave differently on script and on function.
You just have to remove the tic/toc results at the first few iterations.

More Answers (1)

Rik
Rik on 19 Jul 2022
I personally use the ETA_disp function you can find here (or in the AddOn Manager). It only does naive averaging, instead of fancy things like taking the median of the past several calls. For fast loops, calculating statistics takes too much time relative to the calculation itself, while for slow loops, simple averages tend to be surprisingly accurate (or you can skew the n and N to adjust for size). I found this is a decent middle ground.
What you're asking is a sufficiently deep dive that you should consider following the general recommendation: write your code well and let Matlab handle the optimization. If you want accurate measurements of small time intervals, you should also consider using timeit instead of tic,toc. timeit will provide a much more precise indication of runtime, as it measures multiple runs.
cl
tic,foo,t1=toc;tic,cl,t2=toc;t1-t2 % warm up, not required offline
ans = 0.0166
timeit(@foo)-timeit(@cl)
ans = 0.0089
tic,foo,t1=toc;tic,cl,t2=toc;t1-t2
ans = 0.0966
function foo
cl %clear all optimization
i=1;
atan2(sin(1/i),cos(i^2));
end
function cl
clear all
end

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!