In order to increase the performance of our applications, we have to consider loop optimisation techniques during the development phase.
I'd like to show you some different ways to iterate over a simple std::vector<uint32_t> v:
- Unoptimized loop with index: - uint64_t sum = 0; for (unsigned int i = 0; i < v.size(); i++) sum += v[i];
- Unoptimized loop with iterator: - uint64_t sum = 0; std::vector<uint32_t>::const_iterator it; for (it = v.begin(); it != v.end(); it++) sum += *it;
- Cached - std::vector::enditerator:- uint64_t sum = 0; std::vector<uint32_t>::const_iterator it, end(v.end()); for (it = v.begin(); it != end; it++) sum += *it;
- Pre-increment iterators: - uint64_t sum = 0; std::vector<uint32_t>::const_iterator it, end(v.end()); for (it = v.begin(); it != end; ++it) sum += *it;
- Range-based loop: - uint64_t sum = 0; for (auto const &x : v) sum += x;
There are also other means to build a loop in C++; for instance by using std::for_each, BOOST_FOREACH, etc...
In your opinion, which is the best approach to increase the performance and why?
Furthermore, in performance-critical applications it could be useful to unroll the loops: again, which approach would you suggest?
 
     
     
     
     
     
    