在循環中聲明變量是否有任何開銷? (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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章