I was wondering wether the C++ compiler inside Visual Studio 2008 was optimizing it the way it would logically be. So I asked the question on Stackoverflow. It was among my first questions and was to see how the community would answer (yeah I’m lazy).
I was promptly answered by Rob Walker. He showed me what the compiler outputted.
32bit:
1 | 01391000 push ecx |
64bit:
1 | int y = x * 2; |
While this gives us a lot of information about what it does, it’s normally a pretty bad idea to try to outsmart the compiler. The compiler have it’s own way to optimize some operations to make sure that it run best on the targeted platform.
To quote Raymond Chen:
Of course, the compiler is free to recognize this and rewrite your multiplication or shift operation. In fact, it is very likely to do this, because x+x is more easily pairable than a multiplication or shift. Your shift or multiply-by-two is probably going to be rewritten as something closer to an add eax, eax instruction.
The moral of the story is to write what you mean. If you want to divide by two, then write “/2”, not “>>1”
Bold was added by me for emphasis. Raymond Chen wrote that in 2005. Write what you mean. Wow. If you want to multiply then write “*2” and if you want to divide write “/“. If you are meaning to shift bits, then so! But don’t try to outsmart the compiler by doing bit shifting.
If you are doing bit shifting do gain performance, you are doing something wrong. There is many other way to gain performance in the kind of application we, developers, are building and bit shifting is not one of them.
You are simply losing readability for maybe a few milliseconds.