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

前端 未结 4 740
-上瘾入骨i
-上瘾入骨i 2020-12-05 19:01

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

4条回答
  •  我在风中等你
    2020-12-05 19:20

    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
    

提交回复
热议问题