“全球可能效率非常低”

发布于 2024-12-11 23:04:07 字数 825 浏览 0 评论 0原文

我在 if 命令中使用(在 Matlab 中)global 语句,以便仅在确实需要时才将全局变量导入到本地命名空间中。

代码分析器警告我“global 可能非常低效,除非它是其函数中的顶级语句”。考虑到可能的内部实现,我发现这个限制非常奇怪和不寻常。我正在考虑两种可能性:

  1. 这个警告的真正含义是“global本身效率非常低,所以不要在循环中使用它”。特别是,在 if 中使用它,就像我正在做的那样,是完全安全的,并且警告发出错误(并且措辞不佳)

  2. 警告是正确的; 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:

  1. 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)

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

国际总奸 2024-12-18 23:04:07

为了补充 eykanals 帖子,技术说明给出了解释为什么global很慢。

...当函数调用涉及全局变量时,性能会受到更大的抑制。这是因为为了查找全局变量,MATLAB 必须将其搜索空间扩展到当前工作区之外。此外,涉及全局变量的函数调用看起来比其他函数调用慢得多的原因是 MATLAB Accelerator 没有优化此类函数调用。

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.

辞别 2024-12-18 23:04:07

我不知道答案,但我强烈怀疑这与运行时内存的分配和共享方式有关。

尽管如此,我还是建议您阅读 Loren 和 Doug 的 Mathworks 博客 上的以下两篇文章:

  1. 编写可部署代码,他在那篇文章中写的第一件事
  2. 让我哭泣的 10 个 MATLAB 代码实践,该列表中的第 2 个。

长话短说,全局变量几乎永远不是正确的选择。还有许多其他方法可以实现变量共享(她讨论了其中一些方法),这些方法更有效且不易出错。

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.

丿*梦醉红颜 2024-12-18 23:04:07

沃尔特·罗伯森的回答在这里
http://mathworks.com/matlabcentral/answers/19316 -全局可能非常低效#answer_25760

[...] 如果不在顶级命令中完成,这不一定是更多工作,但人们倾向于将构造放在循环中,或者放在条件结构中的多个非排他位置中。对于编写 mlint 警告的人来说,不必添加诸如“除非您可以证明那些“全局””只会执行一次的说明要容易得多,在这种情况下,它的效率并不会降低,但它仍然是不好的形式”

支持我的选择(1)。

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).

心在旅行 2024-12-18 23:04:07

事实(从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文