• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by flaviusxvii · Jun 03, 2011 at 08:26 PM · performancedistancevectorsquare root

Vector*.Distance Performance

I'm starting to do a fair amount of distance checks and normally I'd just use the squared Distances but for some of these checks I need the actual distance. Is the built-in Distance function as fast as something I could write myself. I'm aware of some sqrt approximations that might speed things up but I won't bother if Unity is reasonable fast.

I don't have Unity pro so I can't really instrument and test this and stress testing seems inconclusive so far.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Jun 03, 2011 at 08:37 PM 0
Share

Couldn't you get Time.time, then do like a million calcs, then get Time.time again, subtract, and that gives you execution time?

avatar image Eric5h5 · Jun 03, 2011 at 08:49 PM 0
Share

@DaveA: No, since Time.time won't update, but you can use Time.realtimeSinceStartup for that purpose.

avatar image DaveA · Jun 03, 2011 at 08:54 PM 0
Share

Well there you go! So give that a try and let us know your results.

5 Replies

· Add your reply
  • Sort: 
avatar image
17
Best Answer

Answer by Eric5h5 · Jun 03, 2011 at 09:00 PM

Distance is probably faster than anything you can write yourself. The docs say "Vector3.Distance(a,b) is the same as (a-b).magnitude", but (a-b).magnitude is actually about 25% slower than using Distance, at least on my machine (quad-core Xeon). In fact, Distance isn't actually that much slower than (a-b).sqrMagnitude. (I should also mention a while ago I made an "optimized" square root approximation, but Mathf.Sqrt was actually faster, so I gave up on that....)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flaviusxvii · Jun 03, 2011 at 10:04 PM 0
Share

I guess that's why my stress testing approach wasn't showing much appreciable difference. I'll play with this some more.

avatar image JacobRGames · Oct 10, 2021 at 11:58 PM 0
Share

The thing where you try to make the square root faster. I tried the exact same thing and tested 3 versions of the square root:

  • My custom square root.

  • Unity’s Mathf square root.

  • System square root.

Here are the results that I got, and the funny thing is that the best square root to use is the system square root.

I also find it really funny that not only was my custom square root the slowest it was also a little bit inaccurate.

alt text

avatar image Eno-Khaon JacobRGames · Oct 11, 2021 at 12:47 AM 1
Share

If I'm not mistaken, I believe Unity's Sqrt() function casts the float to a double, runs it through System.Math.Sqrt(), then casts back to a float.

avatar image JacobRGames Eno-Khaon · Oct 11, 2021 at 05:01 AM 0
Share

Yeah, the System square root uses a double, but the conversion for the double into a float is also in the test.

avatar image
11

Answer by ZannaU · Jan 26, 2017 at 06:44 PM

Performance Strongly depend on the platform you are on. As someone mentioned the FPU makes a big difference.

One PC I see Vector3.Distance and Vector3.sqrMagnitude having the same performance. Both faster than Vector3.magnitude. alt text

On mobile (Android Nexus 7) Vector3.Distance is 6X SLOWER that Vector3.sqrMagnitude. alt text

So it depends on the platform you are working on. If you are considering releasing on mobile sqrMagnitude is your safest bet.


distanceperformanceandroid.png (10.2 kB)
distanceperformancepc.png (9.2 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
5

Answer by aldonaletto · Jun 03, 2011 at 09:19 PM

The floating point coprocessor (FPU) inside modern CPUs already have direct square root instructions. Don't mind: these FPUs are fast, incredibly fast. There's no chance to write something faster than a single instruction square root (except look-up tables, and only in some very special cases).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by joihelgi · Jul 03, 2015 at 10:23 PM

My tests show that Vector2.Distance is the same as Vector3.Distance. (vector2 - vector2).sqrMagnitude is about 170% faster than vectorX.Distance on iPhone6 iOS 8.4 - Unity 5.1

(vector2 - vector2).sqrMagnitude is only about 18% faster then (vector3 - vector3).sqrMagnitude on the same device..

For 10.000.000 loops it took about 0.143 sec for the VectorX.Distance and 0.053 sec for (vector2 - vector2).sqrMagnitude

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Matt-Downey · Aug 25, 2012 at 01:17 PM

There is a function that I was referred to which is at least faster on the creator's machine:

http://blog.alladvanced.net/2011/02/21/square-root-calculation-speed-in-flash-and-unity3d/

which I figured out from a question of mine on the forums here: http://forum.unity3d.com/threads/147661-Square-Root-runs-1000-times-in-0.01ms

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

3 vector3 distance checks, only one works 1 Answer

Aim a transform at a point along a vector 0 Answers

Finding Nearest Object Both Positive And Negative? 1 Answer

InverseTransformPoint vs Vector3.Distance which is cheaper? 3 Answers

Calculate the distance between an object and my player 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges