问题
The rider IDE is informing me that the following is inefficient
transform.Translate(moveDirection * speed * Time.smoothDeltaTime);
and wants to re-write it as
transform.Translate(Time.smoothDeltaTime * speed * moveDirection);
Anybody know why ?
Its all multiplications, whats the difference ?
For some context, here is the value of speed and moveDirection
private Vector3 moveDirection = Vector3.left;
private float speed = 2.5f;
I am little confused in understanding why its better ?
Can anyone help?
Thanks
回答1:
Vector3 has 3 components. X, Y and Z.
Multiplying a Vector3 by a value multiplies the components by that value.
Since there are 2 values beside the vector, order matters not for the result, but does for the number of operations.
That is because vector-first will result in 3+3=6 multiplications:
- X*=speed
- Y*=speed
- Z*=speed
- X*=time
- Y*=time
- Z*=time
While vector-last will be 1+3=4 multiplications:
- scale=speed*time
- X*=scale
- Y*=scale
- Z*=scale
Either way, it's just Rider being paranoid about performance, and that level of optimization, while welcome, is definitely not required.
来源:https://stackoverflow.com/questions/57933831/unity-rider-order-of-multiplication-operations-is-inefficient