# Vector3 vs. Vector2 - performance, usage?

I'm currently playing around with XNA, and creating a simple 2D platformer. I was thinking of adding multiple layers to make it a little bit of challenge.

In stead of having a Vector2 for my positions, I now use a Vector3, solely to use it's Z as layer depth. However, since you can't use operators between Vector2 and Vector3 for some unknown reason [1], I ended up changing all other Vector2s in my game, such as acceleration, speed and offset, so I can do things like position += offset without errors.

I also changed my rotation variable from float to Vector3, and I use the Z value to rotate my textures. I'm planning to use the X and Y to scale-flip my textures, so you get the Super Paper Mario effect.

However, after changing all these Vector2s in Vector3s, I felt a little bad about it. How does this effect the performance of games? I know I shouldn't have to worry about performance in my little platformer game, but I'm just curious about it.

Is there any notable performance between Vector2s and Vector3s, for example when adding or multiplying them, or when calling Normalize, Transform, or Distance?

[1] Just a side question, why are there no operators for calculations between Vector3 and Vector2?

Is there any notable performance between Vector2s and Vector3s, for example when adding or multiplying them, or when calling Normalize, Transform, or Distance?

Yes, you have one more coordinate so you will use more CPU cycles.

But it is very unlikely that it will ever give you any trouble. XNA 4 is using SIMD extensions for vector math (EDIT: on Windows Phone only), so the implementation is very optimal (on that platform). Except if you're doing very heavy computing, it is very unlikely to ever cause you trouble. You do need Vector3s for your positions because you're now doing 3D (or 2.5D...), so please don't do any premature optimization. This is 97% evil1.

Just a side question, why are there no operators for calculations between Vector3 and Vector2?

Because it makes no sense, mathematically. What would you expect to come out from such calculations? For instance what should happen if you try to add a Vector3 and a Vector2:

[x1, y1, z1] + [x2, y2] = [x1 + x2, y1 + y2, z1] or [x1, y1 + x2, z1 + y2] ?

In this case, you'll typically need to determine by yourself what you want as a third coordinate for the Vector2, and where you wish to add it. For instance this solves the ambiguity:

[x1, y1, z1] + [x2, y2, 0] = [x1 + x2, y1 + y2, z1]

Now it's possible that some parts of your gameplay work only in 2D. If there are cases where you only need 2D coordinates, and if the computing does get really heavy (e.g. 2D physics), you can stick to Vector2s in that specific part of the code to save some precious cycles. You can then easily switch between 2D and 3D coordinates when you need to (e.g. get a scene position from a 2D physics position, or the other way around):

E.g. from Vector2 to Vector3 using this constructor:

Vector2 v2;
Vector3 v3(v2, someDepthValue);


Or from Vector3 to Vector2 using that constructor;

Vector3 v3;
Vector2 v2(v3.X, v3.Y);


Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.

• Like I said, I know I don't have to worry about performance so soon, but it was just my curiosity that made me post this question. I guess you're right about the ambiguity. Thanks for the clear answer :) – Rudey Jan 25 '13 at 12:27
• @RuudLenders I've added details on how you could proceed if you ever face performance issues. – Laurent Couvidou Jan 25 '13 at 12:43
• I've edited your answer to include a very important point: SIMD is only used by XNA on Windows Phone! The .NET runtime on PC - and therefore XNA itself - does not support SIMD. The equivalent is also not supported on Xbox 360. – Andrew Russell Jan 26 '13 at 9:58
• (On PC and Xbox 360, Understanding XNA Framework Performance is still the applicable guide for optimising your vector operations.) – Andrew Russell Jan 26 '13 at 10:25
• Premature optimisation is not evil. Only useless premature optimisation is. You need to rule it out on the grounds of being useless, not on the grounds of being premature. – sam hocevar Jan 26 '13 at 12:03

You're trying to optimize prematurely. Most of the operations you mentioned ( normalize, transform, distance ) are pretty much identical to what vector2D does, if you can look at their code you will notice that they are practically the same. The only difference is that vector3D has a third axis. Performance wise it should be trivial compared to a Vector2D.

Because you can't multiply matrixes/row-vectors/column-vectors that both have different sizes.

One of the biggest performance effects of using Vector3 unnecessarily, instead of Vector2, is the 50% increase in size and the effect that has on cache.

That unnecessary extra data needs to be loaded into the CPU cache from main memory. This is sloooow.

In addition, by loading in this unnecessary data, you increase the chance that you are pushing out useful data that then immediately has to be loaded back into cache.

In a modestly tight loop, the cache effects will overwhelm any CPU effects of doing extra operations.

Also, it's faster to add the elements directly (due to various quirks of .NET). So if you're micro-optimising you won't be using the vector operations anyway. So if you only need to add the first two elements of a vector, you could do this:

v1.X += v2.X; v1.Y += v2.Y;


But these kinds of performance considerations are only really applicable to things like particle engines, physics engines, and so on. So don't worry too much!

• So is manual inlining still the fastest way to go even with the added support of SIMD? – Mikael Högström Jan 29 '13 at 19:00
• @MikaelHögström SIMD is almost certainly going to be faster - but it is only available on the Windows Phone platform (see the edit and comments I made to Laurent Couvidou's answer). – Andrew Russell Jan 30 '13 at 1:52