From: mordor.nospam@fly.srk.fer.hr   
      
   On 2012-01-15, Dave Harris wrote:   
   >   
   > is no longer too much effort. And it can lead to faster code because it   
   > is easier for a compiler to optimise std::for_each<> than a for-loop,   
   > because it is knows exactly what the former is doing. With the latter, it   
   > has to analyse the loop body to make sure nothing messes with the loop   
   > variable.   
   >   
   I disagree that it can lead to faster code. More precisely: if std::for_each   
   does lead to faster code in practice, the compiler's optimizer is rather   
   worthless. A decent optimizer will analyze the loop body anyway to try to   
   apply far more complex optimizations (e.g., strength reduction) than just   
   determining that the loop body doesn't change the loop variable.   
      
   ==   
      
   Side-note: for (const int i = 0; i < N; ++i) { .. }   
      
   would be a somewhat useful construct with the semantics that the loop variable   
   (not necessarily an integer) can be modified only in the update-part of the   
   for-loop. This is mostly to help the programmer, but could also be a hint to   
   the optimizer.   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|