In particular (the direct cause of this post) is replace this:
Code: Select all
a / 256
Code: Select all
a >> 8
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.
Matthijs