Search Unity

Performance of Vector.normalized

Discussion in 'Scripting' started by Resshin27, Feb 28, 2019.

  1. Resshin27

    Resshin27

    Joined:
    Apr 21, 2018
    Posts:
    31
    Hey,
    I stumbled upon a weird result while doing some random stuff. I compared three different ways of doing Square root operations for calculating a normalized vector all done for 5,000,000 iterations, clocked via Stopwatch() function. The results are:
    1. Vector normalization using Fast Inverse Square Root(1 iteration only of Newton-Raphson): 465ms
    2. Using vector.normalized method of Unity: 592ms
    3. Using vector / Mathf.sqrt(vector.sqrMagnitude): 336ms.

    Now, can someone tell me why using Mathf.Sqrt is the fastest over here? Does Unity implement it using SIMD computations?

    P.S.
    Well I am not going to use vector.normalized for sure :D
     

    Attached Files:

    • Sqrt.jpg
      Sqrt.jpg
      File size:
      102.2 KB
      Views:
      639
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I can't say anything about #1, but those:

    You'll be able to tell the difference when you take a look at the implementation.

    Vector3.normalized calls the static normalization method (method call, vector copy), which then uses the static magnitude function (method call, vector copy) and Mathf.Sqrt internally. It also ensures that it doesn't do nonsesical divisions, e.g. division by zero, in the final step.

    #3 has less overhead, as you calculate it using the property Vector3.sqrMagnitude directly (no further call to a helper function, and no vector copy). The way that you're using to divide vector / magnitude also doesn't take division by zero and other very small values into account.

    So, at least in the editor, you're doing a lot more work using #2. This might be different in build mode, as optimizations can be done and the JIT may behave differently when it comes to inlining etc...

    If you need to process lots of normalizations, there are ways to speed it up even further, for instance, by avoiding the struct's constructor (which Unity doesn't do in their implementations) whenever an operator is called.

    There are also new libraries available on Unity's official repo, which are intended to be used for optimized calculations and which are beneficial when you use the new job system + burst compiler etc... Personally I haven't used those yet, but it might be worth taking a look at.
     
  3. JotaRata

    JotaRata

    Joined:
    Dec 8, 2014
    Posts:
    61
    Did using option #3 worked well for you?
    we need a in-game perfomance status