- Home /
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.
Couldn't you get Time.time, then do like a million calcs, then get Time.time again, subtract, and that gives you execution time?
@DaveA: No, since Time.time won't update, but you can use Time.realtimeSinceStartup for that purpose.
Well there you go! So give that a try and let us know your results.
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....)
I guess that's why my stress testing approach wasn't showing much appreciable difference. I'll play with this some more.
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.
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.
Yeah, the System square root uses a double, but the conversion for the double into a float is also in the test.
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.
On mobile (Android Nexus 7) Vector3.Distance is 6X SLOWER that Vector3.sqrMagnitude.
So it depends on the platform you are working on. If you are considering releasing on mobile sqrMagnitude is your safest bet.
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).
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
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
Your answer
Follow this Question
Related Questions
Find closest "item" object by NavMesh navigation distance performantly 0 Answers
What does bounds.sqrdistance do? 1 Answer
increasing shadow distance for a single object? 1 Answer
Create a vector between me and my enemy. 1 Answer
Cheapest way to get distance detection and triggered events. 2 Answers