- Home /
What is more efficient for assign rotation with euler angles?
So for assign rotation to gameobject with euler angles as input values i can do 2 ways:
1) transform.localRotation = Quaternion.Euler(x, y, z);
2) transform.localEulerAngles = new Vector3(x, y, z);
What way better and why (at performance point of view)?
Debug the Time, run one of them 1 $$anonymous$$illion times, Debug the Time again. do the same thing for the other. compare them.
Answer by Suddoha · Oct 01, 2015 at 09:55 AM
They're both pretty identical from a performance point of view. I've ran a test on my notebook with 10.000 assignments in one frame on keydown. This is, in most cases, far away from the common usage unless you've got a huge project in which this happens alot, like thousands of times.
It yielded 24-26 milliseconds for the first one. The second one yielded 22 milliseconds frequently, in a few cases 23 milliseconds. Tests were repeated a few times just to be sure.
Regarding the amount of assigments, this isn't much of a difference, so to say they're pretty much equal performance-wise (in average for one call: 0,0025 milliseconds = 2.5 microseconds for the first, 2.2 microseconds for the second). This is really nothing to worry about, use the one that you like the most.
An assumption:
The difference could be that the first one does an extra calculation in the property:
calculates a quaternion from the euler angles first in order to get the rotation
assigns the quaternion to the rotation
euler angles are now recalculated in the transform.localRotation property from the new rotation because they might not be stored but calculated on the fly - but that's an implementation details that i don't know
The second one:
just assigns the euler angles
recalculates the quaternion in the localEulerAngles property
So if it's really implemented like that, you'd save one calculation of euler angles.
So variant 2) transform.localEulerAngles = new Vector3(x, y, z); little better.
As for your question, it's nothing to worry about. The one takes (on my laptop) in average about 0.1 to 0.2 microsecond less than the other, that's 1/10000 to 1/5000 milliseconds.
(I corrected the time units, i accidently used nanosecodns ins$$anonymous$$d of microseconds.)
The test is most likely not representative. 24 milliseconds sounds way too much to me.
@Suddoha: What did you use to measure time? Did you restrict your thread to one CPU core? Do you have the transform property within your test or do you use a local variable?
When i run a similar test with 100000 iterations 100 times i get these results:
localEulerAngles: avr: 18.998432ms ($$anonymous$$: 18.6519ms / max: 20.1713ms)
localRotation: avr: 22.733399ms ($$anonymous$$: 22.2308ms / max: 23.3938ms)
localRotation is about 20% slower than localEulerAngles. However i guess it's due to the 1 additional float value being passed around (a quaternion consists of 4 floats) as well as this implementation of the Euler method:
public static Quaternion Euler(float x, float y, float z)
{
return Quaternion.Internal_FromEulerRad(new Vector3(x, y, z) * 0.0174532924f);
}
They first convert degrees to rad as their internal method seems to only work with radians.
edit
$$anonymous$$y benchmark was in the Unity editor. I created a build and i got those results:
localEulerAngles: avr: 14.380257ms ($$anonymous$$: 13.9803ms / max: 15.132ms)
localRotation: avr: 17.36501ms ($$anonymous$$: 16.9045ms / max: 18.2835ms)
Answer by Bunny83 · Oct 01, 2015 at 09:48 AM
Both lines do the exact same thing: converting 3 euler angles floats into a quaternion. Efficiency is completely irrelevant here. Both only use value types (float, Vector3, Quaternion) so no garbage should be created by non of them. "localEulerAngles" is there to simplify getting / setting eulerangles.
Your answer
Follow this Question
Related Questions
Why is gameobject rotating by 90 degrees only once? 1 Answer
turning a project using AddTorque 0 Answers
[C#] Rotate Turret, Only On X and Z Axis 2 Answers
Change mouse rotation point 1 Answer
Player Rotation Movement 1 Answer