“GLOBAL可能是非常低效的” [英] "GLOBAL could be very inefficient"

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

问题描述

我正在使用(在Matlab中)在一个if命令中使用一个全局语句,这样我才能将全局变量导入到本地名称空间中,只要它真的需要。 / p>

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


  1. 这个警告的真正含义是 global 本身效率很低,所以不要在循环中使用它。尤其是,在一个if内部使用它,就像我在做的那样,是完全安全的,并且警告是错误地发布的(而且措辞不佳)

  2. 警告是正确的; Matlab在后台使用了一些非常不寻常的变量加载机制,所以在if语句中导入全局变量的速度真的很慢。在这种情况下,我想提供一个提示或一个指示这些东西是否真正起作用的指针,因为我很感兴趣,而且如果我希望将来编写高效的代码似乎很重要。


这两种解释中的哪一种是正确的? (或者两者都不是?)



预先感谢。编辑:让它更清晰:我知道(code> global )很慢(显然我无法避免使用它,因为它是我正在使用的旧库的设计决策);我问的是为什么Matlab代码分析器抱怨如果(foo == bar)
GLOBAL baz $

  b $ b baz = 1; 
else
do_other_stuff;
end

但不是

  GLOBAL baz 
if(foo == bar)
baz = 1;
else
do_other_stuff;
end

我很难想象为什么第一个应该比第一个要慢第二。

解决方案

Walter Roberson的答案在这里
http://mathworks.com/matlabcentral/answers/19316-global-could-be-very-inefficient#answer_25760



[...]如果没有在顶层命令中完成,这不一定需要更多的工作,但人们往往会倾向于将构造放入循环中,或者放置在条件结构中的多个非独占位置。对于撰写mlint警告的人来说,更容易不必添加如下的澄清:除非你能证明那些全局只能执行一次,在这种情况下,效率并不低,但仍然是不好的形式

支持我的选项(1)。


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.

解决方案

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

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

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