在循环中声明变量是否有任何开销? (C++) - Is there any overhead to declaring a variable within a loop? (C++)

问题:

I am just wondering if there would be any loss of speed or efficiency if you did something like this:我只是想知道如果你做这样的事情是否会降低速度或效率:

int i = 0;
while(i < 100)
{
    int var = 4;
    i++;
}

which declares int var one hundred times.它声明了int var一百次。 It seems to me like there would be, but I'm not sure.在我看来好像会有,但我不确定。 would it be more practical/faster to do this instead:这样做会更实用/更快吗:

int i = 0;
int var;
while(i < 100)
{
    var = 4;
    i++;
}

or are they the same, speedwise and efficiency-wise?或者它们在速度和效率方面是否相同?


解决方案:

参考一: https://en.stackoom.com/question/47iF
参考二: https://stackoom.com/question/47iF
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章