Home > OS >  I cannot understand about the unity documentation explanation of 'Magnitude of Vector'
I cannot understand about the unity documentation explanation of 'Magnitude of Vector'

Time:01-24

Description of Magnitude of Vector3 (or Vector2) says

If you only need to compare magnitudes of some vectors, you can compare squared magnitudes of them using sqrMagnitude (computing squared magnitudes is faster).

But both Vector3.Magnitude and Vector3.sqrMagnitude are public field variables.

I think both Vector3.Magnitude and Vector3.sqrMagnitude are already calculated, so I think there's no speed difference between using Vector3.Magnitude and Vector3.sqrMagnitude. But why documentation says sqrMagnitude is more faster?

CodePudding user response:

Simply look into the source code of Vector3.sqrMagnitude

public float sqrMagnitude 
{ 
    [MethodImpl(MethodImplOptionsEx.AggressiveInlining)] 
    get { return x * x   y * y   z * z; }
}

and Vector3.magnitude

public float magnitude
{
    [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
    get { return (float)Math.Sqrt(x * x   y * y   z * z); }
}

Both are properties which do calculations on demand.

=> magnitude has to additionally take the square root and cast back to float which is more operations => slightly more expensive.


Further what they mean is: For comparing magnitudes (e.g. order by distance) both work the same!

A magnitude is always positive and for positive a and b we know that if

a < b

then also

a² < b²

and wise versa.

=>

  • For simple comparing you want to use sqrMagnitude which is slightly faster
  • You only want to use magnitude if you are really interested in the exact value.

CodePudding user response:

Vector holds just the x, y, z components. Everything else must be computed everytime it is requested. The confusion might come from the fact that Magnitude doesn't look like a function call. However C# has properties and they can have function like logic behind them whenever you access them. That's what's happening here. Everytime you read Magnitude the value is computed and getting the square root is very expensive. Getting it just once you won't notice the time, but if you get it every frame comparing tens or hundreds of vectors you might have a problem, especially on low end hardware.

CodePudding user response:

Magnitude/ sqrMagnitude are properties, and are computed on demand. You can see this in the documentation for Vector3. You can also check the source

That is the natural way to do it. You might have a huge amount of vectors, and they might be very short lived, so you want to minimize construction cost and storage. So you neither want to compute them, nor store them, unless they are actually needed.

But if you really want to know if there is any difference, the best approach is to just test it. Write a minimal program that creates something like 10^8 vectors, and use a stopwatch to check how long time it takes to compute the magnitude of all of them. Do the same for sqrMagnitude and see if there is any difference.

Doing short benchmarks is generally a very useful skill. You might assume that something is faster than another. But assumptions are often wrong, even for experienced developers. So the best approach is to actually test it.

  • Related