> How do i write a code to optimize a given code
1. start with a design
2. choose the best algorithms to implement that design
3. write clear code - if it's obvious to use, then it will be to the compiler.
4. Profile it to make sure you know where the hotspots are
5. Make damn sure it works before you try 6.

6. Fiddle around with all the silly tricks, often repeated without foundation, just because someone found a special case with some compiler 10 years ago, and this technique has become an urban legend in how to optimise code.

Most decent compilers know about all the good optimisations which work (like *2 can be done as <<1, BUT only if its an advantage for that particular machine).