在循环外部声明循环索引变量有什么好处? [英] What's the benefit of declaring for the loop index variable outside the loop?

查看:147
本文介绍了在循环外部声明循环索引变量有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在许多游戏引擎代码中看到这个。这是否应该比如果你在for循环体中声明它更快?此外,还有许多其他的for循环,每个都使用相同的变量。

I see this in a lot of game engine code. Is this supposed to be faster than if you were to declare it in the for loop body? Also this is followed by many other for loops, each of them using the same variable.

int i;
for(i=0; i<count; ++i)
{
}

vs

for(int i=0; i<count; ++i)
{
}

Btw我从来没有这样做,只是想知道背后的想法,因为除了性能我不知道为什么任何人都会这样做。

Btw I never do this myself, just curious about the idea behind it, since apart from performance I don't know why anyone would do this.

推荐答案

第一种方法可能是C方式, OLD OLD C方式,其中第二个版本无效。使用第二种方式,尽可能地缩小变量的范围。

The first way is probably the C way, the OLD OLD C way, where the second version wasn't valid. Use the second way, scope the variables tight as possible.

这篇关于在循环外部声明循环索引变量有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆