I would like to know how to avoid wasting time and risk typos by re-hashing the source code when I integrate legacy code, library code or sample code into my own codebase.
If I give a simple example based on an image processing script, you can see what I mean.
Actually, it’s not unusual for me to integrate a piece of code as follows:
for (unsigned int y = 0; y < uHeight; y++) { for (unsigned int x = 0; x < uWidth; x++) { // do something with this pixel .... uPixel = pPixels[y * uStride + x]; } }
Over time, I got used to doing things like transferring unnecessary calculations from the inner loop and possibly changing postfix increments to prefix ...
for (unsigned int y = 0; y < uHeight; ++y) { unsigned int uRowOffset = y * uStride; for (unsigned int x = 0; x < uWidth; ++x) { // do something with this pixel .... uPixel = pPixels[uRowOffset + x]; } }
Or, I could use pointer arithmetic, either by line ...
for (unsigned int y = 0; y < uHeight; ++y) { unsigned char *pRow = pPixels + (y * uStride); for (unsigned int x = 0; x < uWidth; ++x) { // do something with this pixel .... uPixel = pRow[x]; } }
... or row and column ... so I get something like this
unsigned char *pRow = pPixels; for (unsigned int y = 0; y < uHeight; ++y) { unsigned char *pPixel = pRow; for (unsigned int x = 0; x < uWidth; ++x) { // do something with this pixel .... uPixel = *pPixel++; } // next row pRow += uStride; }
Now, when I write from scratch, I will usually apply my own “optimizations”, but I know that the compiler will also do things like:
- Moving code from inner loops to outer loops
- Change postfix increment to prefix
- Many other things that I have no idea about
Considering that every time I communicate with a piece of working, tested code in this way, I not only cost myself time, but also risked introducing errors with the finger problem or something else (the examples are simplified above). I know about “premature optimization”, as well as other ways to improve performance by developing better algorithms, etc., But for the situations described above, I create building blocks that will be used in larger applications with pipeline types, t predict which may be non-functional requirements, so I just want the code to be fast and tough, as far as reasonable within the time limit (I mean the time spent setting up the code).
So my question is: where can I find out which compiler optimizers are usually supported by "modern" compilers. I use a mixture of Visual Studio 2008 and 2012, but it would be interesting to know if there are differences with alternatives, for example. Intel C / C ++ Compiler. Can someone shed some insight and / or point me to a useful Internet link, book or other link?
EDIT
To clarify my question
- The optimizations shown above were simple examples, not a complete list. I know that it is pointless (in terms of performance) to make those specific changes, because the compiler will do it anyway.
- I am specifically looking for information about which optimizations are provided by the compilers that I use.