Saturday, March 2, 2013

Efficient Vectors in C#

Making the raytracing made me in an efficiency craze. I had to think about the efficiency for everything. One of the basic things to think about is vectors! Consider the following vectors. In this code, there are three vector structs, each with different x, y, z implementations. The three vectors overload the multiplication, addition, and subtraction operators to apply them to x, y, and z. I created a simple test to see the efficiency of the 3 implementations. The test was very simple it involved two vectors one with the x, y, and z equal to PI and vector with the x, y, and z values set to 1. The vector with the 1 value is multiplied, added, and subtracted with a PI vector in a for loop that goes 1,000,000 times. Here are the results:

Struct * + -
Vec
1665
255
316
Vec2
2104
569
781
Vec3
1603
224
346
The result show that properties are the least efficient way, taking 100% more time.