Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

.sqrMagnitude is faster than .magnitude, but I'm unsure if I can use it here/should bother.

Discussion in 'Scripting' started by tfishell, Apr 15, 2020.

  1. tfishell

    tfishell

    Joined:
    Nov 20, 2017
    Posts:
    97
    Edit: I kinda think I should just stick with what I have (setting aside what may be amateurish in the code). "If you only need to compare magnitudes of some vectors, you can compare squared magnitudes of them using sqrMagnitude (computing squared magnitudes is faster)." - I'm doing more than just comparing here.
    ---
    I have a script that, amongst other things, keeps an object within a certain radius. In Update() I get the distance between two points

    Code (CSharp):
    1. centerToAnchor = anchor.position - radiusCenter.position;
    and if a specific object is clicked and held, BoundaryCircle() is run from update.

    Code (CSharp):
    1. //on jonah's controller script, if mouse is on arrow
    2.         if (ArrowController.isPressed)
    3.         {
    4.             //keep arrow within boundary radius
    5.             BoundaryCircle();
    6.         }
    In BoundaryCircle(), I get ".magnitude" to use in clamping an object's position a certain distance from another point.

    Code (CSharp):
    1. void BoundaryCircle()
    2.     {
    3.         float centerToAnchorMag = centerToAnchor[B].magnitude[/B];
    4.          
    5.         Vector3 offset = mousePos - radiusCenter.position;
    6.         Vector3 newPos = radiusCenter.position + Vector3.ClampMagnitude(offset, centerToAnchorMag - inFromAnchor);
    7.  
    8.         newPos.y = Mathf.Clamp(newPos.y, radiusCenter.position.y, Mathf.Infinity);
    9.         newPos.x = Mathf.Clamp(newPos.x, radiusBounds.position.x + inFromAnchor / 2, Mathf.Infinity);
    10.  
    11.         arrow.position = new Vector3(newPos.x, newPos.y, zPos);
    12. ...
    13.     }
    I recently read about how ".sqrMagnitude" is less intensive than ".magnitude", but as I'm not great with math I'm not sure how I'd use it here. It seems overly complex for my situation, like I'd have to square everything then Mathf.Sqrt to find the "original" amount anyways. What I have seems to be working well enough, so I may just stick with what I have, but I wanted to at least ask. Thanks.
     
    Last edited: Apr 15, 2020
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    The rule of thumb for optimizations like this is -- is it currently causing a performance problem? No? Don't worry about it.
     
    Kurt-Dekker and tfishell like this.
  3. tfishell

    tfishell

    Joined:
    Nov 20, 2017
    Posts:
    97
    Haha, all right thanks.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Check this video out, starting around 28:45, as there's some hard numbers about how trivial a square root is in the grand scheme of overall data throughput:



    About a minute later you see what the memory cache hits cost you... :)
     
  5. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    He also said 'premature optimization is the most abused phrase", which I'm sure is correct from where he is coming from, but not neccessarily where we are (unity scripting) :)

    And I absolutely refuse to accept his claim that real-world situations should trump portability concerns (ok, he's phrasing that differently). This probably stems from the fact theat when that talk was held, mobile devices weren't as prevalent as they are today.

    But in general an interesting (if somewhat dated) video that probably gains some actuality now that we have a burst compiler.