Matlab function with initial long execution time

3 views (last 30 days)
Hello,
When I run a function handle its execution time is initially very long but in the second, third, etc runs its execution time is normal (sometimes, however, I see that the lnog initial execution time will happen again in later runs but this does not happen very frequently).I have seen this problem when I work with symbolic expressions but did not expect to see this in matlabfunctions. I would like to know whether there is a way to avoid this? Or, is there something like 'loading the file' (or similar concepts) prior to executing the matlab function to avoid this?
In the following, you see an example about my question (the matlab function EZ_CC_3_11 is attached).
n=10;
Dm0=rand(1,n);Dm1=rand(1,n);Dm0=rand(1,n);Dm2=rand(1,n);Dm3=rand(1,n);Ds0=rand(1,n);Ds1=rand(1,n);Ds2=rand(1,n);Ds3=rand(1,n);
tic;EZ_CC_3_11(Dm0,Dm1,Dm2,Dm3,Ds0,Ds1,Ds2,Ds3);toc;
Elapsed time is 111.170418 seconds.
tic;EZ_CC_3_11(Dm0,Dm1,Dm2,Dm3,Ds0,Ds1,Ds2,Ds3);toc;
Elapsed time is 0.033134 seconds.
tic;EZ_CC_3_11(Dm0,Dm1,Dm2,Dm3,Ds0,Ds1,Ds2,Ds3);toc;
Elapsed time is 0.029289 seconds.
Thanks,
Babak

Accepted Answer

patrick1704
patrick1704 on 25 Jul 2022
Matlab uses a just-in-time compiler that compiles the code at first execution and basically creates the executable code. This is reused for all subsequent evaluation. You can find some documentation here: https://www.mathworks.com/products/matlab/matlab-execution-engine.html
In general, recompilation is only required when something like
clear all;
is used.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by