On 12/6/07, NightStrike <nightstrike@xxxxxxxxx> wrote: > (1) > I am curious about a certain aspect of how the g++ optimizer works at > -O3. If I have a for-loop, there are times when it is beneficial to > have a function call in the while-condition. For instance: > > vector<int> v; > ... > for ( int i=1; i < v.size(); ++i) > > Now, I if v is not changing inside of that for-loop, then this would > appear at first glance to be more efficient: > > int max = v.size(); > for ( int i=1; i < max; ++i) > > By using 'max' instead of v.size(), I am eliminating a function call > from every iteration of the for loop. If the loop is iterated a large > number of times, this could have an advantage. However, is it all the > same when you are optimizing? Does g++ remove the need for the second > method? > > > Before I end this email, I am sure that at least someone is thinking, > "Wait, you're using a vector, and using an old style for loop to > traverse it instead of iterators!" Well, yes, that's true. But I'm > doing that for two reasons -- 1) I'm interested in the question as it > stands, so I asked it the way I did, and 2) My next question will > already cover that, as it involves a comparison between traversing an > array as I do above and using iterators. > > > (2) > Is there a big difference in speed between using a standard for-loop > for vector traversal and using iterators? I already verified that I > can get a significant speed improvement (with very large N) by > accessing the vector directory with v[i] instead of using the at() > method. But what about iterators? If I write it with iterators > instead, will that be optimized out at -O3 to reduce to essentially > the same thing? > I received feedback on #1. Any ideas on #2 from this email?