本文介绍了内循环变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这是做里面循环数组的一些操作的功能。每次迭代后它应该阵列初始化为零。因此,这下面将给出一个性能优势?

I have a function which is doing some operation on array inside loop. After every iteration it is supposed to initialize the array to zero. So which of the following will give a performance benefit?

one.c

int main(void){
 char buf[4096] = { 0};

 while (1 /*flag*/) {

       /*Some operation here*/
    memset (buf, 0, sizeof (buf));
    }
}

two.c

int main(void){

 while (1 /*flag*/) {
       char buf[4096] = { 0};
       /*Some operation here*/
    }
}

在one.c明确memset的被使用。但在two.c数组声明内循环,并初始化为零。

In one.c explicit memset is used. But in two.c array is declared inside the loop and initialized to zero.

推荐答案

这些都是完全一样的。事实上这两个程序可以完全优化掉,因为没有任何产生看得见的行为。

These are both exactly the same. In fact both programs could be fully optimized out since none produces any observable behaviour.

如果你的编译器(最大优化级别)做了不同的两个程序,然后可以向您的编译器供应商,并且还使用哪一个似乎根据你的环境中进行测试,以更快。

If your compiler (at maximum optimization level) does something differently for either program then could report it to your compiler vendor, and also use whichever one seems to be faster based on testing in your environment.

没有全球性的回答什么是速度更快。

There is no global answer to "what is faster".

,我在一些克鲁夫特以抑制优化 - FUNC1 FUNC2 机构类似于pretty。

Link to example code where I added in some cruft to inhibit optimization - func1 and func2 bodies look pretty similar.

这篇关于内循环变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 22:29
查看更多