這是微型優化,還是它是最優化的?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