After seeing some patches, I noticed that a lot of people tend to over-optimize here and there.
In particular (the direct cause of this post) is replace this:
a / 256
a >> 8
The argument is, that bitshifting is a lot faster that dividing. I won't argue with that, because it is. But, I believe that making this optimization makes the code less readable, which is something we should prevent.
Besides that, this optimization is quite trivial and will allways be made by the compiler (I tested, even gcc -O0 does it).
There are probably other optmisations like this, though I can't recall anything from the top of my head right now. To prevent the code becoming even more unreadable because of these things, I propose the following policy:
- The only kind of optmization that can be applied, is optmization in the algorithms or data structures used. Other optimisations that simply replace one piece of code by another, semantically similar one, should be handled by the compiler. Anything that isn't too complex for you, won't be too complex for the compiler either.
- If any optmization is applied it should always be commented. No Exceptions.
- If any optimization is applied, include the original code in comments.
A hint here is to just code something the way you would want it and when applying optimization, comment out the original and replace it by the optimized code.
What do we think of this?