性能明智的速度有多快位运算符与正常模 [英] Performance wise how fast Are Bitwise operator vs. normal modulus
问题描述
if(i++ & 1){
}
VS
if(i % 2){
}
可以使用正常的流量或条件之类的语句为
,如果<
按位
操作/ code>等提高性能,这将是更好
尽可能总是使用它们呢?
Can using Bitwise
operation in normal flow or conditional statements like for
, if
etc increase the performance and would it be better
to always use them where possible?
推荐答案
除非你正在使用的是古老的编译器,它已经可以处理这个水平上了自己的转换。也就是说,一个现代化的编译器可以和将实施我%2
使用按位和
指令,只要它使感觉这样做的目标CPU(其中,公平地说,它通常会)。
Unless you're using an ancient compiler, it can already handle this level of conversion on its own. That is to say, a modern compiler can and will implement i % 2
using a bitwise AND
instruction, provided it makes sense to do so on the target CPU (which, in fairness, it usually will).
在换句话说,不希望看到的任何的这些之间的性能,至少在一个比较现代的编译器,一个有能力的优化差别。在这种情况下,合理
有太多一个pretty宽泛的定义 - 甚至相当多的编译器是几十年的历史可以处理这种微型的优化,没有在困难所有
In other words, don't expect to see any difference in performance between these, at least with a reasonably modern compiler with a reasonably competent optimizer. In this case, reasonably
has a pretty broad definition too--even quite a few compilers that are decades old can handle this sort of micro-optimization with no difficulty at all.
这篇关于性能明智的速度有多快位运算符与正常模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!