开发者

Performance of vector assignment after function

I use this code:开发者_开发百科

MainLoop() {
    for (int i = 0; i < length; i++) {
        XMVector3Rotate(rays[i], orientation);
    }
}

and I have fps 1900000, but when I use this one:

MainLoop() {
    for (int i = 0; i < length; i++) {
        calculatedRays[i] = XMVector3Rotate(rays[i], orientation);
    }
}

I have fps = 200. Why?


When you are doing this:

XMVector3Rotate(rays[i], orientation);

I am guessing that the compiler inlines the function - and sees that, because its result is never asigned anywhere - it doesn't actually do anything, and removes the function call completely. It's very fast because it's not actually doing anything.

But then when you add in the assignment:

calculatedRays[i] = XMVector3Rotate(rays[i], orientation);

All of a sudden you're doing a bunch of memory reads and writes and various maths operations - all of which were being skipped over before.

(You had tagged this XNA -- but this is a C++ function. Most C++ compilers can and will inline functions like this. The standard C# compiler cannot.)


In the first example the result of the function is being discarded right away (not being assigned). The compiler is smart enough to sense that, and omits the method call...


Assuming XMVector3Rotate returns a XNA Vector3 type, this will be a struct copy operation, relatively costly in performance.

When optimizing my own XNA game for the XBox360, I replaced many such operations by ref params type, with a very noticeable gain in heavy loops.

EDIT: example (from memory)

Vector3 vec1 = something, vec2 = something, result;
Vector3.Add(ref vec1, ref vec2, out result);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜