- Home /
Performance of Vector Addition vs. Component Addition
Hey Unity! The Vector3
is a mutable data type with respect to its components. If you have, say,
Vector3 a = Vector3.up;
Vector3 b = Vector3.right;
You can get the additive resultant as either
return a + b;
or
return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
but which one is faster? I'm inclined to think that the internal implementation of the first option is just the second option, and in any case I doubt if the first option can be slower than the second, but the docs for the addition operation don't specify, and as far as I know it is technically possible that it's instead implemented
Vector3 operator+(const Vector3& other) {
this->x += other.x;
this->y += other.y;
this->z += other.z;
return this;
}
which should be faster than the second option since you're not going out of your way to allocate memory for a brand new Vector3 object.
Note that the source code of the UnityEngine.dll is available for reference over here. Note that it's only up for reference. $$anonymous$$ake sure you read the licence and copyright information given in the readme.
Second you seem to confuse C# and C++ code as well as objects vs structs. Vector3 is a struct and therefore a value type. The constructor of the Vector3 struct does not allocate any memory.
...Y-yeah! That sounds about right actually. Whups. Can't ask questions when tired I guess, I couldn't even wrap my head around an array at the time > >
Answer by Hellium · Jun 30, 2018 at 08:55 PM
Here is the implementation of the +
operator overload in the Unity engine.
// Adds two vectors.
public static Vector3 operator+(Vector3 a, Vector3 b) { return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z); }
The problem with the code you suggest is that one of the two vectors involved in the operation will be irremediably changed. However, when you do 1 + 2
, 1
and 2
don't change, it wouldn't be logical if it happened with the +
operator of the Vector
struct.
But, keep in mind that your concerns should not exist. This is micro-optimisation your user will never notice, unless you compute hundreds of vectors each frame.
You could probably replace hundreds with thousands or millions ^^. Though i've actually done a test on manually inlining code and got a massive speed improvement.(8x faster). Though this was only tested inside the editor. In an actual build the small operators / methods may be inlined automatically. Though manually expanding / inlining the code in my case allowed for some additional simplifications which is probably the main reason for the speed up.
The main reason of the speed up is the lack of call to the vector3's constructor. You can find more detail about that subject in the link I posted bellow. And the optimization works also in a build
Ooooh you're right! That was a silly of me. pff
Answer by Aka_ToolBuddy · Jun 09, 2020 at 07:42 PM
You might find useful an asset that I made that implements optimizations like the one you posted about. You can find more about the subject here: https://forum.unity.com/threads/vector3-and-other-structs-optimization-of-operators.477338 The actual asset is : Frame Rate Booster
Your answer

Follow this Question
Related Questions
EnemyHover Script Error? 0 Answers
Calculate vector exactly between 2 vectors 4 Answers
Tic Tac Toe--how to check for win 2 Answers
How to smooth my camera (Vector3.Lerp) 0 Answers
Vector3 Projection 2 Answers