Unity / RIDER: order of multiplication operations is inefficient?

不问归期 提交于 2019-12-11 19:07:30

问题


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:

  1. X*=speed
  2. Y*=speed
  3. Z*=speed
  4. X*=time
  5. Y*=time
  6. Z*=time

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!