Joa*_*nge 0 c# c++ python optimization performance
我在代码库和在线中也看到了很多这种风格,如果你有一个带有for循环的函数和if语句,那么只有它们使用的所有变量都没有在它们之外声明.例如:
void process()
{
int i;
int count = 100;
vector3 point;
vector sum;
for (i = 0; i < count; ++i)
{
import(this, "pos", point);
sum += point;
}
sum /= count;
}
Run Code Online (Sandbox Code Playgroud)
或者这是不成熟的优化?我对C++,C#和Python很感兴趣,这些是我使用的语言,我一遍又一遍地看到它们.
很多旧代码都是这样做的,因为它在C89/90中是必需的.好吧,技术上,从来没有要求变量在函数的开头定义,只在块的开头.例如:
int f() {
int x; // allowed
x = 1;
int y; // allowed in C++, but not C89
{
int z=0; // beginning of new block, so allowed even in C89
// code that uses `z` here
}
}
Run Code Online (Sandbox Code Playgroud)
C++从未有过这样的限制(C也没有相当长的一段时间),但是对于一些旧的习惯很难.对于其他人来说,保持代码库的一致性超过了定义接近其使用位置的变量的好处.
就优化而言,这通常都不会产生任何影响.
| 归档时间: |
|
| 查看次数: |
131 次 |
| 最近记录: |