   Inverse optimization?
  Ok, now here''s a stupid question.I know that to get faster code, you keep the most useless code outside of loops, like branches (if) and calculations.However, in my code, doing this is both crucial and leads to tons of repeating code. Now my question is this:Is there any way to like do your code the most stupid way, but to tell the compiler (MSVC7 in this case) to just optimize these ifs outside of the loop???The reason is I''m making my graphics wrapper and I have to deal with inputs and outputs of 16, 24 and 32 bits, which leads to about 9 cases for each function I write.Thanks 
quote:Original post by persil  I know that to get faster code, you keep the most useless code outside of loops, like branches (if) and calculations.Yes and no. To get faster code, keep lengthy processes outside your inner loops (and precalculate whenever you can relative to the inner loop).Finish the engine, then worry about optimization. Dijkstra''s Law: Premature optimization is the root of all evil. ;  Typically, humans are extremely bad at guessing where micro-optimization is needed.  They are quite good at determining where macro-optimization is needed though.  Make sure you select appropriate data structures and algorithms, write your code, get it working, and look for performance problems.Perf DOES have to be considered from the start if it''s going to be a limiting factor later on.  The ability to make that judgement is one that comes with experience.  For most projects, it''s probably not going to matter ;  First thanks for the replies.Now there''s a short example which I''m currently scratching my head on.do{  switch (inputbpp)  {  case 32:  ...  case 16:  ...  case 24:  ...  }  DoPixelConversion()  switch (outputbpp)  {  case 32:  ...  case 16:  ...  case 24:  ...  }}while (++x < dwXStop);Now that''s the really unoptimized form and I know this. However it is really the easiest to maintain form, and I know that it''s pretty obvious for the compiler to figure out how to optimize this.I''m wondering if the compiler is actually optimizing this loop by itself, because if it is, I won''t get into the trouble of splitting the code in 9 separate cases, which if I''m wrong by only one bit, I''ll have to go through the 9 copy+paste and have 9 times more risk of making mistakes.Of course, I can always make the whole thing run sloppy and do that copy+pasting thing afterwards when the whole project has been tested and tested, but I don''t like that idea very much either. ;PixelType Convert24to16(PixelType & p){  // body} PixelType Convert32to16(PixelType & p){  // body} PixelType Convert16to24(PixelType & p){  // body} PixelType Convert32to24(PixelType & p){  // body} PixelType Convert16to32(PixelType & p){  // body} PixelType Convert24to32(PixelType & p){  // body} typedef PixelType (*CONVERTFUNC)(PixelType &); CONVERTFUNC Select(const int in, const int out){  if( in == 16 )  {    if( out == 24 ) return Convert16to24;    else if( out == 32 ) return Convert16to32;    else return 0;  }  else if( in == 24 )  {    // etc  }  else if( in == 32 )  {    // etc  }  else    return 0;}Selecting and using the conversion functions should be intuitive. 