c# - 团结/骑士: order of multiplication operations is inefficient?

标签 c# unity3d rider

Rider IDE 通知我以下内容效率低下

        transform.Translate(moveDirection * speed * Time.smoothDeltaTime);

并想将其重写为
        transform.Translate(Time.smoothDeltaTime * speed * moveDirection);

有人知道为什么吗?

都是乘法,有什么区别?

对于某些情况,这里是 speed 和 moveDirection 的值
private Vector3 moveDirection = Vector3.left;

private float speed = 2.5f;

我有点不明白为什么它更好?

任何人都可以帮忙吗?

谢谢

最佳答案

Vector3 有 3 个组件。 X、Y 和 Z。
将 Vector3 乘以一个值,就是将组件乘以该值。
由于向量旁边有 2 个值,因此顺序与结果无关,但与操作次数有关。
那是因为向量优先将导致 3+3=6 次乘法:

  • X*=速度
  • Y*=速度
  • Z*=速度
  • X*=时间
  • Y*=时间
  • Z*=时间

  • 虽然 vector-last 将是 1+3=4 乘法:
  • 规模=速度*时间
  • X*=比例
  • Y*=比例
  • Z*=比例

  • 无论哪种方式,这只是 Rider 对性能的偏执,而这种级别的优化虽然总是受欢迎,但在大多数情况下绝对不是必需的。

    关于c# - 团结/骑士: order of multiplication operations is inefficient?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57933831/

    相关文章:

    c# - IEnumerable<string> 到 FileStreamResult 的 Stream

    c# - LINQ 中的关系划分?

    c# - 静态只读字段的初始化顺序

    c# - 我如何调试远程机器上的程序?

    c# - 将对象转换为子类型? (沮丧)

    resharper - 在 Rider 和 ReSharper 之间共享代码样式设置

    c# - 在Unity中动态改变物体的速度

    c# - 如何在知道方向、角度和距离的情况下找到位置?

    windows-authentication - JetBrain Rider,将 IIS Express 与 Windows 身份验证结合使用时访问被拒绝

    c# - Microsoft.Extensions.DependencyInjection : DI'd code runs fine on dotnet CLI/Rider default . NET 配置文件,但导致 VS2022 崩溃