“GLOBAL could be very inefficient”

后端 未结 4 572
有刺的猬
有刺的猬 2020-12-04 01:56

I am using (in Matlab) a global statement inside an if command, so that I import the global variable into the local namespace only if it is really needed.

4条回答
  •  無奈伤痛
    2020-12-04 02:27

    To supplement eykanals post, this technical note gives an explanation to why global is slow.

    ... when a function call involves global variables, performance is even more inhibited. This is because to look for global variables, MATLAB has to expand its search space to the outside of the current workspace. Furthermore, the reason a function call involving global variables appears a lot slower than the others is that MATLAB Accelerator does not optimize such a function call.

提交回复
热议问题