“全球可能非常低效" [英] "GLOBAL could be very inefficient"

查看:22
本文介绍了“全球可能非常低效"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 if 命令中使用(在 Matlab 中)一个 global 语句,以便我仅在真正需要时才将全局变量导入本地命名空间.

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.

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

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. 这个警告的真正意思是global 本身效率很低,所以不要在循环中使用它".特别是,在 if 中使用它,就像我正在做的那样,是完全安全的,并且警告发出错误(措辞不当)

  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)

警告是正确的;Matlab 在后台使用了一些非常不寻常的变量加载机制,因此在 if 语句中导入全局变量确实要慢得多.在这种情况下,我想对这些东西的实际工作方式有一个提示或指示,因为我很感兴趣,而且如果我以后想编写高效的代码,这似乎很重要.

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

提前致谢.

为了更清楚:我知道 global 很慢(显然我无法避免使用它,因为它是我正在使用的旧库的设计决策);我要问的是为什么 Matlab 代码分析器会抱怨

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

但不是关于

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.

推荐答案

Walter Roberson 的回答在这里http://mathworks.com/matlabcentral/answers/19316-global-可能非常低效#answer_25760

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

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

[...] 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"

支持我的选项(1).

这篇关于“全球可能非常低效"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆