我可以用和吗?
我的教授曾经说过,如果陈述相当缓慢,应尽可能避免。我在OpenGL中制作了一款游戏,在那里我需要很多游戏。 在我的测试中,用短路和通过短路的命令替代了,但是速度更快吗?
bool doSomething();
int main()
{
int randomNumber = std::rand() % 10;
randomNumber == 5 && doSomething();
return 0;
}
bool doSomething()
{
std::cout << "function executed" << std::endl;
return true;
}
我的目的是在我的渲染器的拉动功能中使用它。我的模型应该具有标志,如果标志为真,则应执行某个功能。
My prof once said, that if-statements are rather slow and should be avoided as much as possible. I'm making a game in OpenGL, where I need a lot of them.
In my tests replacing an if-statement with AND via short-circuiting worked, but is it faster?
bool doSomething();
int main()
{
int randomNumber = std::rand() % 10;
randomNumber == 5 && doSomething();
return 0;
}
bool doSomething()
{
std::cout << "function executed" << std::endl;
return true;
}
My intention is to use this inside the draw function of my renderer. My models are supposed to have flags, if a flag is true, a certain function should execute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是错误和/或误导。关于程序缓慢的最简化的语句是错误的。这个答案也可能有问题。
C ++语句没有可以归因于它们的速度。重要的是编译程序的速度。这包括集会语言指示;不是C ++语句。
可能更正确的是说分支指令可以相对较慢(在现代,超级量表CPU架构上)(当无法很好地预测分支时)(根据您的比较;很多东西要贵得多)。
if statement通常被编译成使用分支指令的程序。短路逻辑和操作通常也被编译为使用分支指令的程序。用逻辑和操作员替换IF-Statement并不是使程序更快的神奇子弹。
如果要比较逻辑和相应的程序所产生的程序,该程序被
替换为
,如果(RandomNumber == 5)
,您会发现优化者可以通过您的技巧看到并产生在这两种情况下,相同的组装。为了避免分支,您必须更改前提。您可以创建一个应调用该函数的模型的序列,而不是通过所有模型迭代,检查标志并调用一个函数,而是可以将其调用,迭代该函数并无条件地调用函数 - &gt;没有分支。这替代速度更快吗?肯定有一些维护数据结构的开销,并且分支预测因子可能使这是不必要的。唯一可以肯定的方法是测量程序。
This is wrong and/or misleading. Most simplified statements about slowness of a program are wrong. There's probably something wrong with this answer too.
C++ statements don't have a speed that can be attributed to them. It's the speed of the compiled program that matters. And that consists of assembly language instructions; not of C++ statements.
What would probably be more correct is to say that branch instructions can be relatively slow (on modern, superscalar CPU architectures) (when the branch cannot be predicted well) (depending on what you are comparing to; there are many things that are much more expensive).
An if-statement is often compiled into a program that uses a branch instruction. A short-circuiting logical-and operation is also often compiled into a program that uses a branch instruction. Replacing if-statement with a logical-and operator is not a magic bullet that makes the program faster.
If you were to compare the program produced by the logical-and and the corresponding program where it is replaced with
if (randomNumber == 5)
, you would find that the optimiser sees through your trick and produces the same assembly in both cases.In order to avoid the branch, you must change the premise. Instead of iterating through a sequence of all models, checking flag, and conditionally calling a function, you could create a sequence of all models for which the function should be called, iterate that, and call the function unconditionally -> no branching. Is this alternative faster? There is certainly some overhead of maintaining the data structure and the branch predictor may have made this unnecessary. Only way to know for sure is to measure the program.
我同意上面的评论,几乎在所有实际情况下,如果您毫不犹豫地使用 s,都可以使用
。
s,使用逻辑运算符可能会发出类似于我也同意,对于初学者而言,浪费能量在优化时并不重要,并且如果
的代码。
但是 - 这里有一个与分支有关的有效问题,因此欢迎有兴趣的人阅读。
现代CPU使用我们所谓的指令poipeelining 。
。
不会太挑剔的技术细节:
在每个CPU核心内都有一个并行性。
每个组件指令由几个阶段组成,在执行当前指令时,在一定程度上准备了Next 指令。
这就是所谓的说明。
这个概念一般都在任何类型的分支中打破,有条件的( s)尤其是。
的确,有一种
因此,尽管在大多数情况下,如果 s完全可以,则应考虑其中的情况。
与往常一样,当涉及到优化时,应该仔细介绍。
以以下代码为例(在图像处理和其他实现中很常见类似的代码):
这样做可能会更好(即使它包含了从软件工程角度来看的重复,这是邪恶的):
我们仍然如果,请具有
,但它可能只能分支一次。
在某些[罕见]情况下(需要如上所述进行分析),这样做更有效的是:
我知道这不是常见的,但是几年前我遇到了特定的HW,这是2个乘法的成本和1次的成本否定小于分支成本(由于指令管道的重置)。此外,此“技巧”支持使用不同条件值的数据的不同部分。
底线:
如果
通常可以,但是很高兴知道有时会有成本。I agree with the comments above that in almost all practical cases, it's OK to use
if
s as much as you need without hesitation.I also agree that it is not an issue important for a beginner to waste energy on optimizing, and that using logical operators will likely to emit code similar to
if
s.However - there is a valid issue here related to branching in general, so those who are interested are welcome to read on.
Modern CPUs use what we call Instruction pipelining.
Without getting too deap into the technical details:
Within each CPU core there is a level of parallelism.
Each assembly instruction is composed of several stages, and while the current instruction is executed, the next instructions are prepared to a certain degree.
This is called instruction pipelining.
This concept is broken with any kind of branching in general, and conditionals (
if
s) in particular.It's true that there is a mechanism of branch prediction, but it works only to some extent.
So although in most cases
if
s are totally OK, there are cases it should be taken into account.As always when it comes to optimizations, one should carefully profile.
Take the following piece of code as an example (similar things are common in image processing and other implementations):
It might be better to do it like this (even though it contains duplication which is evil from software engineering point of view):
We still have an
if
but it's causing to branch potentially only once.In certain [rare] cases (requires profiling as mentioned above) it will be more efficient to do even something like this:
I know it's not common , but I encountered specific HW some years ago on which the cost of 2 multiplications and 1 addition with negation was less than the cost of branching (due to reset of instruction pipeline). Also this "trick" supports using different condition values for different parts of the data.
Bottom line:
if
s are usually OK, but it's good to be aware that sometimes there is a cost.