From: NightStrike <nightstrike@gmail.com>
To: gcc-help <gcc-help@gcc.gnu.org>
Subject: Optimiziation questions (c++)
Date: Thu, 06 Dec 2007 17:59:00 -0000 [thread overview]
Message-ID: <b609cb3b0712060959v45201064u3dfbc6714ee9c277@mail.gmail.com> (raw)
(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?
next reply other threads:[~2007-12-06 17:59 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-06 17:59 NightStrike [this message]
2007-12-06 20:31 ` Brian Dessent
2008-02-07 3:41 ` NightStrike
2008-02-07 16:01 ` Tony Wetmore
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b609cb3b0712060959v45201064u3dfbc6714ee9c277@mail.gmail.com \
--to=nightstrike@gmail.com \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).