First of all, you can use the switch here:
switch(a) { case 0: // handle a==0 break; case 1: // handle a==1 break; default: // handle all other cases }
This may allow the compiler to produce faster code, i.e. Perform one calculated leap, not multiple checks against a .
this would mean copying a multitude of things common to both situations.
Refactor ! How about adding common code to a separate function, maybe declare it inline and hope the compiler follows the prompt? The inlining function is a good way to allow the compiler to duplicate code (the other two methods are templates and a preprocessor, both of which are obviously not suitable here).
inline void sharedStuff() {...} int myloop(int a, .....){ if (a==1) { while(...){
Of course, it depends on what you do in your cycle, but you should get the basic principle.
And last but not least: profile . Check if the loop is really a performance bottleneck. Look at the generated machine code. Most likely, the compiler already uses most of the proposed optimizations.
source share