I'm currently writing an application which needs to perform as good as it can be. I'm using VS2012 as my IDE (and compiler).
I've noticed, as I'm iterating over vectors, that a pointer solution in contrast to iterator, is much quicker in terms of speed. Why is that? I mean, an iterator is basicly the same thing. It should be at least.
Take a look at this example:
std::vector<int> v;
for (std::vector<int>::iterator it = v.begin(); it != v.end(); it++) {
// Do stuff with *it
}
for (int* i = &v[0], i <= &v[v.size()-1]; i++) {
// Do stuff with *i
}
Is there any optimization that is needed by the compiler? I'm using all the standard settings, so -O2 is already enabled. Thanks in advance.
EDIT: I'm compiling in Release, and running with Ctrl+F5 (without debugging that is).
EDIT2: The actual source-code is an implementation of quicksort. Here is a link to the full source, its very short so check it out.
I'm currently writing an application which needs to perform as good as it can be.
Then grab a profiler and look where the real bottlenecks are. In optimized code (Release mode), of course.
-O2 is not everything in VS2012: there are several #defines
that manipulate the behavior of standard container iterators wrt to bounds checking and other security checks. You might want to look them up ("checked iterators" and "secure SCL" might lead you to the right sites) and set them accordingly.
But I very much doubt that the iteration over containers will be your bottleneck, there will be other sections of code that are more sensitive to performance issues.