Why does Matlab run faster after a script is “warmed up”?

醉酒当歌 提交于 2019-12-17 16:04:16

问题


I have noticed that the first time I run a script, it takes considerably more time than the second and third time1. The "warm-up" is mentioned in this question without an explanation.

Why does the code run faster after it is "warmed up"?

I don't clear all between calls2, but the input parameters change for every function call. Does anyone know why this is?


1. I have my license locally, so it's not a problem related to license checking.

2. Actually, the behavior doesn't change if I clear all.


回答1:


One reason why it would run faster after the first time is that many things are initialized once, and their results are cached and reused the next time. For example in the M-side, variables can be defined as persistent in functions that can be locked. This can also occur on the MEX-side of things.

In addition many dependencies are loaded after the first time and remain so in memory to be re-used. This include M-functions, OOP classes, Java classes, MEX-functions, and so on. This applies to both builtin and user-defined ones.

For example issue the following command before and after running your script for the first run, then compare:

[M,X,C] = inmem('-completenames')

Note that clear all does not necessarily clear all of the above, not to mention locked functions...

Finally let us not forget the role of the accelerator. Instead of interpreting the M-code every time a function is invoked, it gets compiled into machine code instructions during runtime. JIT compilation occurs only for the first invocation, so ideally the efficiency of running object code the following times will overcome the overhead of re-interpreting the program every time it runs.




回答2:


Matlab is interpreted. If you don't warm up the code, you will be losing a lot of time due to interpretation instead of the actual algorithm. This can skew results of timings significantly.

Running the code at least once will enable Matlab to actually compile appropriate code segments.




回答3:


Besides Matlab-specific reasons like JIT-compilation, modern CPUs have large caches, branch predictors, and so on. Warming these up is an issue for benchmarking even in assembly language.

Also, more importantly, modern CPUs usually idle at low clock speed, and only jump to full speed after several milliseconds of sustained load.

Intel's Turbo feature gets even more funky: when power and thermal limits allow, the CPU can run faster than its sustainable max frequency. So the first ~20 seconds to 1 minute of your benchmark may run faster than the rest of it, if you aren't careful to control for these factors.




回答4:


Another issue not mensioned by Amro and Marc is memory (pre)allocation.
If your script does not pre-allocate its memory it's first run would be very slow due to memory allocation. Once it completed its first iteration all memory is allocated, so consecutive invokations of the script would be more efficient.

An illustrative example

for ii = 1:1000
    vec(ii) = ii; %// vec grows inside loop the first time this code is executed only
end


来源:https://stackoverflow.com/questions/16873733/why-does-matlab-run-faster-after-a-script-is-warmed-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!