Why does Matlab run faster after a script is “warm

2019-01-14 15:58发布

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.

4条回答
来,给爷笑一个
2楼-- · 2019-01-14 16:43

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.

查看更多
对你真心纯属浪费
3楼-- · 2019-01-14 16:43

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楼-- · 2019-01-14 16:57

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
查看更多
够拽才男人
5楼-- · 2019-01-14 17:01

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.

查看更多
登录 后发表回答