Unity/RIDER:乘法运算的顺序效率低下吗? [英] Unity / RIDER: order of multiplication operations is inefficient?

查看:298
本文介绍了Unity/RIDER:乘法运算的顺序效率低下吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

骑士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:

  1. X * =速度
  2. Y * =速度
  3. Z * =速度
  4. X * =时间
  5. Y * =时间
  6. Z * =时间

vector-last将是1 + 3 = 4乘法:

While vector-last will be 1+3=4 multiplications:

  1. scale = speed * time
  2. X * =规模
  3. Y * =规模
  4. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆