Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...