我应该移位以在 Java 中除以 2 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4072703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:17:02  来源:igfitidea点击:

Should I bit-shift to divide by 2 in Java?

javaperformancebit-manipulationdivisionbit-shift

提问by Matt Huggins

Possible Duplicates:
Is shifting bits faster than multiplying and dividing in Java? .NET?
Quick Java Optimization Question

可能的重复:
在 Java 中移位是否比乘法和除法更快?。网?
快速 Java 优化问题

Many years ago in college, I learned that bit-shifting right by one accomplishes the same thing as dividing by two, but is generally significantly faster. I'm not sure how Java has come along in that regards since the 9-10 years ago I learned about that. Does the Java compiler automatically converts a divide-by-two into a bit-shift operation, or should I manually perform the bit-shift operation in the code myself?

多年前在大学时,我了解到右移一位与除以 2 的效果相同,但通常要快得多。我不确定自从 9 到 10 年前我了解到 Java 在这方面是如何发展的。Java 编译器是否会自动将除以二转换为位移操作,还是应该我自己在代码中手动执行位移操作?

采纳答案by Paul Sasik

Unless you're working in a shop and a codebase where bit-shifting is common then, IMHO, you're risking obfuscation. Yes, the expressions may be logically equivalent but:

除非你在一个商店和一个代码库中工作,否则位移很常见,恕我直言,你冒着混淆的风险。是的,这些表达式在逻辑上可能是等价的,但是:

  • A n00b might get confused by the alternate syntax
  • An old guy who hasn't had to do any bit-shifting since college, like myself, might get confused
  • If you bit shift and feel the need to comment on what you just did then you're definitely off. Simple division is self-documenting and would be clear to anyone who's familiar with elementary math
  • You're not going to outsmart a compiler for optimization on something that simple so don't bother trying
  • As good coding practice it's better to make your code simple/vanilla rather than clever(er)
  • n00b 可能会被替代语法混淆
  • 像我这样从大学起就没有做过任何位转换的老家伙可能会感到困惑
  • 如果你有点改变并且觉得有必要对你刚刚所做的事情发表评论,那么你肯定会离开。简单的除法是自我记录的,任何熟悉初等数学的人都会很清楚
  • 你不会比编译器更聪明地优化一些简单的东西,所以不要费心去尝试
  • 作为良好的编码实践,最好使您的代码简单/香草而不是聪明(呃)

All this is relative and, again, really depends on your shop's standards. If your colleagues love to bit-shift, then by all means go forth and bit-shift.

所有这些都是相对的,同样,真的取决于您商店的标准。如果您的同事喜欢进行位移,那么一定要进行位移。

回答by Malfist

The division routine for your CPU will handle this. There is no need for you to do it.

CPU 的除法例程将处理此问题。你没有必要这样做。

This is known as a premature optimization.

这被称为过早优化

回答by Rémi

Modern compilers are clever enough to generate the fastest code for divisions by two. They'll do a shift if it is faster. If what you want to achieve is a division by 2, using a division will make your code clearer. And you'll avoid problems when the number to be divided is negative.

现代编译器足够聪明,可以生成最快的除以二的代码。如果速度更快,他们会换班。如果你想要实现的是除以 2,使用除法会让你的代码更清晰。当被除数为负数时,您将避免出现问题。

回答by Michael Borgwardt

Yes, this is the very first thing that anyone attempting to do compiler optimizations will do (and has done for at least 5 decades), it most certainly is done by the Java JIT compiler, and you'd probably have a very hard time finding any compiler that doesn't do it.

是的,这是任何尝试进行编译器优化的人都会做的第一件事(并且至少已经做了 5 年),它肯定是由 Java JIT 编译器完成的,您可能很难找到任何不这样做的编译器。

And even if they didn't, it would still be a premature micro-optimization that should be avoided in favor of having the code be clearer.

即使他们没有这样做,这仍然是一个过早的微优化,应该避免,以便让代码更清晰。