Search code examples
c++performancedirectxxna-math-library

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?


Solution

  • 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.)