Unity/RIDER:乘法运算的顺序效率低下吗? [英] Unity / RIDER: order of multiplication operations is inefficient?
问题描述
骑士IDE通知我以下内容无效
The rider IDE is informing me that the following is inefficient
transform.Translate(moveDirection * speed * Time.smoothDeltaTime);
,并希望将其重写为
transform.Translate(Time.smoothDeltaTime * speed * moveDirection);
有人知道为什么吗?
所有乘法,有什么区别?
Its all multiplications, whats the difference ?
对于某些情况,这是speed和moveDirection的值
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 ?
任何人都可以帮忙吗?
谢谢
推荐答案
Vector3具有3个组成部分. X,Y和Z.
Vector3 has 3 components. X, Y and Z.
将Vector3乘以一个值,即可将分量乘以该值.
Multiplying a Vector3 by a value multiplies the components by that value.
由于向量旁边有2个值,因此顺序与结果无关,而与操作数无关.
Since there are 2 values beside the vector, order matters not for the result, but does for the number of operations.
那是因为矢量优先将导致3 + 3 = 6乘法:
That is because vector-first will result in 3+3=6 multiplications:
- X * =速度
- Y * =速度
- Z * =速度
- X * =时间
- Y * =时间
- Z * =时间
vector-last将是1 + 3 = 4乘法:
While vector-last will be 1+3=4 multiplications:
- scale = speed * time
- X * =规模
- Y * =规模
- Z * =比例
无论哪种方式,都只是Rider对性能抱有偏执,而优化级别(尽管受到欢迎)绝对不是必需的.
Either way, it's just Rider being paranoid about performance, and that level of optimization, while welcome, is definitely not required.
这篇关于Unity/RIDER:乘法运算的顺序效率低下吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!