这是微型优化,还是它是最优化的?C++这是微型优化的一种形式
void Renderer::SetCamera(FLOAT x, FLOAT y, FLOAT z) {
// Checking for zero before doing addition?
if (x != 0) camX += x;
if (y != 0) camY += y;
if (z != 0) camZ += z;
// Checking if any of the three variables are not zero, and performing the code below.
if (x != 0 | y != 0 | z != 0) {
D3DXMatrixTranslation(&w, camX, camY, camZ);
}
}
会运行一个具有vector.size()条件的for循环强制应用程序重新计算每个循环中向量中的元素?
std::vector<UINT> vect;
INT vectorSize = vect.size();
for (INT Index = 0; Index < vectorSize; Index++) {
// Do vector processing
}
// versus:
std::vector<UINT> vect;
for (INT Index = 0; Index < vect.size(); Index++) {
// Do vector processing
}
我使用Visual Studio和作为第二个问题,它看起来像是一个编译器可以优化,但我只是不知道这一点。
如果这些值大部分时间都是“int”并且为零,它会不会反效果呢?我的意思是,检查和分支似乎并不比加入零更快。 – zakinster 2013-04-29 17:03:59
@zakinster,如果他们大部分时间都是int并且为零,那真的取决于架构。我和你一起认为最可能的是继续前进并添加它,但你永远不知道所有(奇怪)的体系结构。 – Shahbaz 2013-04-29 17:06:01
即使在大多数情况下它是'int'并且为零,如果'camX'上的缓存读取错误接近100%,它只会更快。 @zakinster – dialer 2013-04-29 17:08:46