C ++运算符/简单数学的速度

问题描述:

我正在研究物理引擎,并认为它将有助于更好地了解执行许多简单或复杂的数学运算的速度和性能效果.

I'm working on a physics engine and feel it would help having a better understanding of the speed and performance effects of performing many simple or complex math operations.

  1. 物理引擎的很大一部分正在淘汰不必要的计算,但是在什么时候计算足够小以至于不需要进行比较检查?

  • 例如:测试两个线段是否相交.在直接进行简单数学运算之前是否应该检查它们是否彼此靠近,否则从长远来看,额外的操作会减慢该过程吗?

不同的数学计算需要花费多少时间

  • 例如:(3 + 8)vs(5x4)vs(log(8))等.

不平等检查需要花费多少时间?

  • 例如:>,<,=

  1. 您将必须进行性能分析.

  1. You'll have to do profiling.

基本操作(如加法或乘法)仅应使用一条asm指令.

Basic operations, like additions or multiplications should only take one asm instructions.

根据注释,尽管采用一个asm指令,但乘法可以扩展为微指令.

As per the comments, although taking one asm instruction, multiplications can expand to microinstructions.

对数花费更长的时间.

也有一条asm指令.

除非您对代码进行概要分析,否则无法确定瓶颈所在.

Unless you profile your code, there's no way to tell where your bottlenecks are.

除非您数百万次调用数学运算(甚至可能会调用数学运算),否则选择算法或进行其他一些高级优化将比优化小算法带来更大的速度提升.

Unless you call math operations millions of times (and probably even if you do), a good choice of algorithms or some other high-level optimization will results in a bigger speed gain than optimizing the small stuff.

您应该编写易于阅读且易于修改的代码,并且只有在对性能不满意的情况下,才开始进行优化-首先是高级,然后才是低级.

You should write code that is easy to read and easy to modify, and only if you're not satisfied with the performance then, start optimizing - first high-level, and only afterwards low-level.

您可能还想尝试动态编程或缓存.

You might also want to try dynamic programming or caching.