Author Topic: use Vector3.sqrMagnitude instead of Vector3.magnitude  (Read 6100 times)

nekokiller

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
use Vector3.sqrMagnitude instead of Vector3.magnitude
« on: March 11, 2014, 10:53:47 AM »
hi, ngui team:

You should use the Vector3.sqrMagnitude instead of Vector3.magnitude.

Our program with ngui , the performance is not good  on the android/ios device . ( our case, we got fps = 40 ~ 45 )

We change the ngui source code , use sqrMagnitude instead of magnitude , then the performance is become good a lot ~ ( fps up to 50 ~ 55 )

« Last Edit: March 11, 2014, 11:05:11 AM by nekokiller »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: use Vector3.sqrMagnitude instead of Vector3.magnitude
« Reply #1 on: March 11, 2014, 04:40:15 PM »
sqrMagnitude and magnitude are quite different. You can't just replace everything. NGUI doesn't use .magnitude at run-time in a lot of places. Scroll view mainly, and the event position checks which occur only when there is an active touch / mouse event. So I don't know what it is you changed, but my guess is that you broke something and the increased performance was just a side-effect.

nekokiller

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: use Vector3.sqrMagnitude instead of Vector3.magnitude
« Reply #2 on: March 11, 2014, 08:59:51 PM »
thanks for reply.

we are not just replace the 'magnitude', of couse we have to modify relative codes to make the logic right.

Vector3.sqrMagnitude :  Returns the squared length of this vector

If you just want to check the distance is bigger or smaller than a constant value, not to get a real distance , you really really should use sqrMagnitude instead of magnitude for the performance issue.

The reason for using sqrMagnitude instead of magnitude is because magnitude has to do a square root operation, which is pretty expensive, much more so than a multiplication.


example :

UICamera.cs : void ProcessTouch( ... )
...
float mag = currentTouch.totalDelta.magnitude;
...
if ( currentTouch.delta.magnitude != 0f )
...

example :

LookAtTarget.cs : LateUpdate() :
...
float mag = dir.magnitude ;
if(mag > 0.001f )
...



change to currentTouch.delta.sqrMagnitude is not better ?  of course the sqrMagnitude is better than magnitude.


other examples in
UIDragObject.cs, UIWidget.cs, UIScrollView.cs


« Last Edit: March 12, 2014, 12:05:08 AM by nekokiller »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: use Vector3.sqrMagnitude instead of Vector3.magnitude
« Reply #3 on: March 11, 2014, 11:50:24 PM »
Interesting... I'll remove .magnitude usage from UICamera. Any other places?

nekokiller

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: use Vector3.sqrMagnitude instead of Vector3.magnitude
« Reply #4 on: March 12, 2014, 12:12:11 AM »

UIDraggableCamera.cs : ConstrainToBounds( ) :  if ( offset.magnitude > 0f )

UIScrollView.cs : RestrictWithinBounds( ) : if (constraint.magnitude > 1f )

UIPanel.cs : ConstrainTargetToBounds()

SpringPosition.csd : Update( ) :    use sqrMagnitude can do the same result, just have to modify some codes.



ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: use Vector3.sqrMagnitude instead of Vector3.magnitude
« Reply #5 on: March 12, 2014, 09:45:30 PM »
Got them, thanks.