Java的:是否按位OR和AND比同等逻辑运算更快? [英] Java: Are bitwise OR and AND FASTER than the equivalent logical operators?

查看:158
本文介绍了Java的:是否按位OR和AND比同等逻辑运算更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添油加醋......虽然我从来没有足够的逻辑操作为它是一个性能瓶颈 - 我不知道,我会过得更好使用按位与(安培)及按位或(|),而不是相同-named逻辑运算符(安培;&放大器;和||)如果可能的话?也许这个问题可以是事实,我不知道库到Java转换为汇编看操作#的pfaced $ P $。

Cut and dry... while I never have enough logical operations for it to be a performance bottleneck - I wonder, would I be better off using bitwise and (&) and bitwise or (|) as opposed to the same-named logical operators (&& and ||) if possible? Perhaps the question can be prefaced by the fact that I don't know of a library to convert Java to assembly to see the # of operations.

推荐答案

位运算符避免分支指令,即便在Java中code执行。因此,你有没有昂贵的分支prediction缺失和不跳的。

Bitwise operators avoid branching instructions, even in Java code execution. As a result you have no expensive branch prediction misses and no jumps at all.

从我的经验,他们的可以的将可测量快于code使用时执行往往不够。请记住,虽然,按位运算符不短路的人,实际上可能在某些情况下,对性能产生负面影响。

From my experience, they can be measurably faster when used in code that is executed often enough. Keep in mind, though, that the bitwise operators are not short-circuiting ones, which may actually have a negative impact on performance in some cases.

这是说,这种微型优化只应作为最后的手段,只有分析器告诉你这样做之后 - 可读性和可维护性至上

That said, such micro-optimizations should only be used as a last resort and only after a profiler tells you to do so - readability and maintainability comes first.

这篇关于Java的:是否按位OR和AND比同等逻辑运算更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆