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

发布于 2024-09-30 19:48:37 字数 467 浏览 8 评论 0原文

可能的重复:
Java 中的移位速度比乘法和除法更快? .NET?
快速 Java 优化问题

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我是男神闪亮亮 2024-10-07 19:48:37

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

  • n00b 可能会对替代语法感到困惑
  • 一个从大学开始就不需要进行任何位移位的老家伙,比如我自己,可能会感到困惑
  • 如果你进行位移位并觉得有必要评论你刚刚做了什么,那么你肯定就完了。简单的除法是自我记录的,对于任何熟悉初等数学的人来说都是清楚的,
  • 你不会比编译器更聪明地优化这么简单的东西,所以不要费心去尝试
  • 作为良好的编码实践,最好让你的代码简单/香草而不是聪明(呃)

所有这些都是相对的,而且,实际上取决于您商店的标准。如果你的同事喜欢进行位移,那么一定要继续进行位移。

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)

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.

陈独秀 2024-10-07 19:48:37

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

即使他们不这样做,这仍然是一种不成熟的微优化,应该避免这种优化,以便让代码更清晰。

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.

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.

千紇 2024-10-07 19:48:37

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

这称为过早优化

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

This is known as a premature optimization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文