n >> 3 或 n & 3 相对于 n/8 或 n % 8 分别有什么优势吗?

huangapple go评论80阅读模式
英文:

Does n >> 3 or n & 3 offer any advantage over n/8 or n % 8 respectively?

问题

在我的程序中,内存和CPU时间都是约束条件,而且这个计算会每秒大约执行50000次。如果使用位运算符代替算术运算,是否会获得更好的性能?

英文:

In my program memory and cpu time are constraints also this calculation will be done around 50000 times every second. Will there be a performance gain if bitwise operators are used over arithmetic ?

答案1

得分: 3

以下是翻译好的内容:

几乎不太可能会有任何差异;对于这种事情,CPU 几十年来都不会关心。

一般来说,如果你在没有任何实际迹象表明性能不达标之前就担心性能问题,那么你将会度过不愉快的时光。现代硬件和 JVM 的优化代码非常复杂,即使是 JVM 性能工程师们自己也记录下来,他们很难仅仅通过查看代码然后猜测是否可以通过便宜的技巧(如尝试用位移替代除法)来加快速度。

解决方案很简单,就是绝对不要涉及这种情况:如果你有性能需求,将它们写下来,使用性能分析工具来找出需要查看的地方(因为通常情况下,99% 的 CPU 资源都花在了 1% 或更少的代码上——所以在开始性能测量之前,你需要知道要测量什么)。

一旦你知道了,使用 JMH 来实际测试性能。这就是它的用途。

如果 JMH 告诉你位移操作更快(我非常怀疑),请知道这个结果未必适用于其他 CPU 架构。

英文:

It is highly unlikely that it would make any difference; CPUs wouldn't care about this sort of thing for decades.

In general, if you're worried about performance before you have any actual indication that the performance is below your needs – you're going to have a bad time. Modern hardware, and the JVM's optimising code is so incredibly complicated, even the JVM performance engineers themselves are on record that they have a very hard time just looking at code and then guessing if it can be made faster with cheap tricks like trying to replace a division by a bitshift.

The solution is to simply never engage in that sort of thing: If you have performance needs, write them down, and use profilers to figure out where to look (because generally 99% of the CPU resources are spent on 1% or less of the code – so before starting performance measurements, you need to know what to measure).

Once you know, use JMH to actually test performance. That's what it is for.

IF JMH tells you that the bitshift is faster (I highly doubt it), know that this result does not necessarily translate to other CPU architecture.

答案2

得分: 1

以下是翻译好的内容:

通过进行一些简单的测试,我的观察表明是的,这确实会产生影响。不,我没有使用JMH,所以我知道会有一些反对意见。但无论测试顺序如何,位操作总是被观察到比较快。我不能确定更快是否等于优势,但在没有证明其他情况之前,我在可能的情况下将继续偏爱它们。

  • (i&1) == 1i%2 == 1 更快
  • i>>3i/8 更快

我以前在API代码中见过这种情况,据记录是更快的,但我还没有尝试过。

  • a<<6 + a<<5 + a<<2 vs a*100

然后关于位移还有这个。

算术基准

英文:

My observations of doing some simple testing would indicate yes, it makes a difference. No I didn't use JMH so I know I'll get some push back. But regardless of the order of testing the bitwise operations were always observed to be faster. I can't say if faster equates to advantage but until proven otherwise I will continue to favor them when possible.

  • (i&1) == 1 is faster than i%2 == 1
  • i>>3 is faster than i/8

And I have seen this in the API code before, documented as being faster but I haven't tried it.

  • a<<6 + a<<5 + a<<2 vs a*100

And then there is this for the bit shifting.

Arithemetic Benchmark

huangapple
  • 本文由 发表于 2020年3月16日 08:02:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/60698889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定