Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

c++ - Are bitwise operators faster?, if yes then why?

How much will it affect the performance if I use:

n>>1 instead of n/2
n&1 instead of n%2!=0
n<<3 instead of n*8
n++ instead of n+=1
and so on...

and if it does increase the performance then please explain why.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Any half decent compiler will optimize the two versions into the same thing. For example, GCC compiles this:

unsigned int half1(unsigned int n) { return n / 2; }
unsigned int half2(unsigned int n) { return n >> 1; }
bool parity1(int n) { return n % 2; }
bool parity2(int n) { return n & 1; }
int mult1(int n) { return n * 8; }
int mult2(int n) { return n << 3; }
void inc1(int& n) { n += 1; }
void inc2(int& n) { n++; }

to

half1(unsigned int):
        mov     eax, edi
        shr     eax
        ret
half2(unsigned int):
        mov     eax, edi
        shr     eax
        ret
parity1(int):
        mov     eax, edi
        and     eax, 1
        ret
parity2(int):
        mov     eax, edi
        and     eax, 1
        ret
mult1(int):
        lea     eax, [0+rdi*8]
        ret
mult2(int):
        lea     eax, [0+rdi*8]
        ret
inc1(int&):
        add     DWORD PTR [rdi], 1
        ret
inc2(int&):
        add     DWORD PTR [rdi], 1
        ret

One small caveat is that in the first example, if n could be negative (in case that it is signed and the compiler can't prove that it's nonnegative), then the division and the bitshift are not equivalent and the division needs some extra instructions. Other than that, compilers are smart and they'll optimize operations with constant operands, so use whichever version makes more sense logically and is more readable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...