“GLOBAL could be very inefficient”

后端 未结 4 488
有刺的猬
有刺的猬 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:17

    The answer from Walter Roberson here http://mathworks.com/matlabcentral/answers/19316-global-could-be-very-inefficient#answer_25760

    [...] This is not necessarily more work if not done in a top-level command, but people would tend to put the construct in a loop, or in multiple non-exclusive places in conditional structures. It is a lot easier for a person writing mlint warnings to not have to add clarifications like, "Unless you can prove those "global" will only be executed once, in which case it isn't less efficient but it is still bad form"

    supports my option (1).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-04 02:30

    I do not know the answer, but I strongly suspect this has to do with how memory is allocated and shared at runtime.

    Be that as it may, I recommend reading the following two entries on the Mathworks blogs by Loren and Doug:

    1. Writing deployable code, the very first thing he writes in that post
    2. Top 10 MATLAB code practices that make me cry, #2 on that list.

    Long story short, global variables are almost never the way to go; there are many other ways to accomplish variable sharing - some of which she discusses - which are more efficient and less error-prone.

    0 讨论(0)
  • 2020-12-04 02:32

    Fact(from Matlab 2014 up until Matlab 2016a, and not using parallell toolbox): often, the fastest code you can achieve with Matlab is by doing nested functions, sharing your variables between functions without passing them.

    The step close to that, is using global variables, and splitting your project up into multiple files. This may pull down performance slightly, because (supposedly, although I have never seen it verified in any tests) Matlab incurs overhead by retrieving from the global workspace, and because there is some kind of problem (supposedly, although never seen any evidence of it) with the JIT acceleration.

    Through my own testing, passing very large data matrices (hi-res images) between calls to functions, using nested functions or global variables are almost identical in performance.

    The reason that you can get superior performance with global variables or nested functions, is because you can avoid having extra data copying that way. If you send a variable to function, Matlab does so by reference, but if you modify the variable in the function, Matlab makes a copy on the fly (copy-on-write). There is no way I know of to avoid that in Matlab, except by nested functions and global variables. Any small drain you get from hinderance to JIT or global fetch times, is totally gained by avoiding this extra data copying, (when using larger data).

    This may have changed with never versions of Matlab, but from what i hear from friends, I doubt it. I cant submit any test, dont have a Matlab license anymore.

    As proof, look no further then this toolbox of video processing i made back in the day I was working with Matlab. It is horribly ugly under the hood, because I had no way of getting performance without globals.

    This fact about Matlab (that global variables is the most optimized way you can code when you need to modify large data in different functions), is an indication that the language and/or interpreter needs to be updated.

    Instead, Matlab could use a better, more dynamic notion of workspace. But nothing I have seen indicates this will ever happen. Especially when you see the community of users seemingly ignore the facts, and push forward oppions without any basis: such as using globals in Matlab are slow.

    They are not.

    That said, you shouldnt use globals, ever. If you are forced to do real time video processing in pure Matlab, and you find you have no other option then using globals to reach performance, you should get the hint and change language. Its time to get into higher performance languages.... and also maybe write an occasional rant on stack overflow, in hopes that Matlab can get improved by swaying the oppinions of its users.

    0 讨论(0)
提交回复
热议问题