public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Optimiziation questions (c++)
@ 2007-12-06 17:59 NightStrike
  2007-12-06 20:31 ` Brian Dessent
  2008-02-07  3:41 ` NightStrike
  0 siblings, 2 replies; 4+ messages in thread
From: NightStrike @ 2007-12-06 17:59 UTC (permalink / raw)
  To: gcc-help

(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?

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-02-07 16:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-06 17:59 Optimiziation questions (c++) NightStrike
2007-12-06 20:31 ` Brian Dessent
2008-02-07  3:41 ` NightStrike
2008-02-07 16:01   ` Tony Wetmore

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).