Looping statements performance and pre-allocating the looping statement itself

℡╲_俬逩灬. 提交于 2019-12-01 18:10:27

This finding has nothing to do with preallocating or not: it deals with matlab being enable or not to compute things with several cores. When you insert the colon operator within the for statement, it tells matlab to use several cores (i.e. multithreading).

If you set matlab on one core only with feature('accel','off'), the observed difference with doubles vanishes. Concerning cells, matlab does not make use of multithreading - therefore no difference can be observed (whatever the status of accel is).

The for loop is multithreaded when a colon is used, and only if a colon is used. The following vectors of similar length does not engage several cores:

  • for k = randperm(N)
  • for k = linspace(1,N,N)

but for k = 1:0.9999:N is multithreaded.

One explanation can be found on this matlab's support page. It states that multi-core processing can be done when "The operations in the algorithm carried out by the function are easily partitioned into sections that can be executed concurrently.". With a colon operator, Matlab knows that for can be partitioned.

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