“全球可能效率非常低”
我在 if 命令中使用(在 Matlab 中)global
语句,以便仅在确实需要时才将全局变量导入到本地命名空间中。
代码分析器警告我“global
可能非常低效,除非它是其函数中的顶级语句”。考虑到可能的内部实现,我发现这个限制非常奇怪和不寻常。我正在考虑两种可能性:
这个警告的真正含义是“
global
本身效率非常低,所以不要在循环中使用它”。特别是,在 if 中使用它,就像我正在做的那样,是完全安全的,并且警告发出错误(并且措辞不佳)警告是正确的; Matlab 在后台使用了一些非常不寻常的变量加载机制,因此在 if 语句中导入全局变量确实要慢得多。在这种情况下,我希望有一个提示或指针来说明这些东西是如何真正工作的,因为我很感兴趣,并且如果我想将来编写高效的代码,它似乎很重要。
这两种解释哪一种是正确的? (或者也许两者都不是?)
提前致谢。
编辑:为了更清楚地说明:我知道 global
很慢(显然我无法避免使用它,因为这是我正在使用的旧库的设计决策);我要问的是为什么Matlab代码分析器会抱怨
if(foo==bar)
GLOBAL baz
baz=1;
else
do_other_stuff;
end
而不是
GLOBAL baz
if(foo==bar)
baz=1;
else
do_other_stuff;
end
我发现很难想象为什么第一个应该比第二个慢的原因。
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.
The code analyzer warns me that "global
could be very inefficient unless it is a top-level statement in its function". Thinking about possible internal implementation, I find this restriction very strange and unusual. I am thinking about two possibilities:
What this warning really means is "
global
is very inefficient of its own, so don't use it in a loop". In particular, using it inside an if, like I'm doing, is perfectly safe, and the warning is issued wrongly (and poorly worded)The warning is correct; Matlab uses some really unusual variable loading mechanism in the background, so it is really much slower to import global variables inside an if statement. In this case, I'd like to have a hint or a pointer to how this stuff really works, because I am interested and it seems to be important if I want to write efficient code in future.
Which one of these two explanations is correct? (or maybe neither is?)
Thanks in advance.
EDIT: to make it clearer: I know that global
is slow (and apparently I can't avoid using it, as it is a design decision of an old library I am using); what I am asking is why the Matlab code analyzer complains about
if(foo==bar)
GLOBAL baz
baz=1;
else
do_other_stuff;
end
but not about
GLOBAL baz
if(foo==bar)
baz=1;
else
do_other_stuff;
end
I find it difficult to imagine a reason why the first should be slower than the second.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了补充 eykanals 帖子,此技术说明给出了解释为什么global很慢。
To supplement eykanals post, this technical note gives an explanation to why global is slow.
我不知道答案,但我强烈怀疑这与运行时内存的分配和共享方式有关。
尽管如此,我还是建议您阅读 Loren 和 Doug 的 Mathworks 博客 上的以下两篇文章:
长话短说,全局变量几乎永远不是正确的选择。还有许多其他方法可以实现变量共享(她讨论了其中一些方法),这些方法更有效且不易出错。
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:
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.
沃尔特·罗伯森的回答在这里
http://mathworks.com/matlabcentral/answers/19316 -全局可能非常低效#answer_25760
支持我的选择(1)。
The answer from Walter Roberson here
http://mathworks.com/matlabcentral/answers/19316-global-could-be-very-inefficient#answer_25760
supports my option (1).
事实(从 Matlab 2014 到 Matlab 2016a,并且不使用并行工具箱):通常,使用 Matlab 可以实现的最快代码是通过执行嵌套函数,在函数之间共享变量而不传递它们。
接近此的步骤是使用全局变量,并将项目拆分为多个文件。这可能会稍微降低性能,因为(据说,虽然我从未在任何测试中验证过它)Matlab 通过从全局工作区检索而产生开销,并且因为存在某种问题(据说,尽管从未见过任何证据) )与 JIT 加速。
通过我自己的测试,使用嵌套函数或全局变量在函数调用之间传递非常大的数据矩阵(高分辨率图像)在性能上几乎相同。
您可以通过全局变量或嵌套函数获得卓越的性能,因为您可以避免通过这种方式复制额外的数据。如果您向函数发送变量,Matlab 将通过引用执行此操作,但如果您修改函数中的变量,Matlab 会动态复制(写入时复制)。据我所知,除了嵌套函数和全局变量之外,在 Matlab 中没有办法避免这种情况。由于 JIT 或全局获取时间的阻碍而产生的任何小消耗都可以通过避免这种额外的数据复制来完全获得(当使用较大数据时)。
这可能在 Matlab 的从未版本中发生了变化,但从我从朋友那里听到的消息来看,我对此表示怀疑。我无法提交任何测试,不再拥有 Matlab 许可证。
作为证明,不用再看这个 视频处理工具箱是我在使用 Matlab 时制作的。它的底层非常丑陋,因为如果没有全局变量,我就无法获得性能。
关于 Matlab 的这一事实(当您需要在不同函数中修改大数据时,全局变量是您可以编码的最优化方式)表明语言和/或解释器需要更新。
相反,Matlab 可以使用更好、更动态的工作空间概念。但我没有看到任何迹象表明这种情况会发生。特别是当你看到用户社区似乎忽视事实,并在没有任何依据的情况下提出意见时:例如在 Matlab 中使用全局变量很慢。
他们不是。
也就是说,您永远不应该使用全局变量。如果您被迫在纯 Matlab 中进行实时视频处理,并且您发现除了使用全局变量来达到性能之外别无选择,您应该得到提示并更改语言。是时候进入更高性能的语言了......并且也可能偶尔写一篇关于堆栈溢出的咆哮,希望 Matlab 能够通过影响用户的意见来得到改进。
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.