performance - c++ for range loop quicker than decrementing loop -


We often loop start arrays such as the last chance: < using pre> static const char * to pszStrings [] = {...} (size_t i = _countof (pszStrings); i--;) or (pszStrings [i]);

My question is that if the new category is very efficient to use the loop and it needs to be used:
  static const char * PszStrings [] = {...} (auto string: pszStrings) use (string);   

extra ?? | | |

Since I can not see the generated code, and even if I do not know, then be able to make the right conclusions, I would be happy to have many scientific answers!

compiler new Range for-loop a lot about the repeated collection in C Could create a notion, which was in the first transcribed-loop. Example:

Generally write one in C ++ 98 days:

 for  (std :: vector : const_iterator it = V.begin (), it_end = V.end (); it! = It_end; ++ it) {// code ..}   

it_end The use was done because in many cases if the compiler can not be sure that the collection can not change the shape inside the loop (and .end () ). If you type:

 for  (std :: vector : const_iterator it = v.begin (); it! = V.end (); + + it) {// code ..}   

The compiler can generate a call to the .end () function for each execution of the loop.

The loop for the range is equally efficient (some times more efficient), which is for the old loop.

Some examples:

If you have loop-ietating by a std :: vector index: for (int i = 0; i & lt; v.size (); ++ i) element in the loop will need to vector index ( v [i] ) to use the most is not necessarily there for There is no waiting to be noticeable, but when the vector is really big, there may be a notice in a tight loop.

Such a loop will be updated when is updated to the range in the loop .

In the answer the loop will not improve performance, but clearly, the code reader will know that the code should be iterative all the archives and do not leave some elements (that old way t, you can recognize each pattern loop, but it is tedious and modifying the index or iterator to the wrong conclusion (perhaps body of code) If not, be careful Yes.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -