简体   繁体   English

为什么if语句和变量声明比循环中的加法更快?

[英]Why is a if statement and a variable declaration faster than a addition in a loop?

if we have if statements with declarations of variables like this: 如果我们有if语句声明变量如下:

#include <iostream>
#include <ctime>

using namespace std;

int main() {

    int res = 0;

    clock_t begin = clock();
    for(int i=0; i<500500000; i++) {
        if(i%2 == 0) {int fooa; fooa = i;}
        if(i%2 == 0) {int foob; foob = i;}
        if(i%2 == 0) {int fooc; fooc = i;}
    }
    clock_t end = clock();
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;

    cout << elapsed_secs << endl;

    return 0;
}

the result is: 结果是:

1.44

Process returned 0 (0x0)   execution time : 1.463 s
Press any key to continue.

but, if it is: 但是,如果是:

#include <iostream>
#include <ctime>

using namespace std;

int main() {

    int res = 0;

    clock_t begin = clock();
    for(int i=0; i<500500000; i++) {
        res++;
        res--;
        res++;
    }
    clock_t end = clock();
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;

    cout << elapsed_secs << endl;

    return 0;
}

the result is: 结果是:

3.098

Process returned 0 (0x0)   execution time : 3.115 s
Press any key to continue.

why does adding or subtracting takes more time to run than a if statement with a variable declaration? 为什么加法或减法比带有变量声明的if语句需要更多的时间来运行?

The difference is almost certainly due to compiler optimization. 差异几乎可以肯定是由于编译器优化。 You'll have to look at the assembly to make sure, but here is my take on what happens: 你必须看看装配以确保,但这是我对发生的事情的看法:

In the first example it's trivial for the optimizer to realize that the bodies of the if s have no effect. 在第一个例子中,优化器认识到if的主体没有效果是微不足道的。 In each a variable local to the if is declared, assigned to and immediately destroyed. 在每个变量本地的if变量被声明,赋值并立即销毁。 So the if s get optimized away, leaving an empty for loop that gets optimized away as well. 因此if被优化掉了,留下了一个空的for循环也被优化了。

The sitiuation in the second example is not that trivial on the whole. 第二个例子中的情境并非整体而言微不足道。 What is trivial is that the body of the loop boils down to a single res++ , which most likely will be further optimized to ++res . 什么微不足道的是,循环体归结为一个单一的res++ ,其中最有可能将进一步优化++res But because res is not local to the loop the optimizer has to consider the whole main() function to realize that the loop has no effect. 但是因为res不是循环本地的,所以优化器必须考虑整个main()函数来实现循环没有效果。 Most likely it fails to do so. 很可能它没有这样做。

Conclusion: In its current form the measurements are meaningless. 结论:在目前的形式下,测量结果毫无意义。 Disabling optimization also won't help because you'll never do that for a production build. 禁用优化也无济于事,因为您永远不会为生产构建执行此操作。 If you really want to dive into this I suggest watching CppCon 2015: Chandler Carruth "Tuning C++: Benchmarks, and CPUs, and Compilers! Oh My!" 如果你真的想深入研究这个我建议观看CppCon 2015:Chandler Carruth“调优C ++:基准测试,CPU和编译器!哦,我的!” for great advice on how to handle the optimizer in these kinds of siutuations. 关于如何在这些类型的siutuations中处理优化器的好建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM