This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Optimiziation questions (c++)
- From: Brian Dessent <brian at dessent dot net>
- To: NightStrike <nightstrike at gmail dot com>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Thu, 06 Dec 2007 12:30:50 -0800
- Subject: Re: Optimiziation questions (c++)
- References: <b609cb3b0712060959v45201064u3dfbc6714ee9c277@mail.gmail.com>
- Reply-to: gcc-help <gcc-help at gcc dot gnu dot org>
NightStrike wrote:
> 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?
The compiler can hoist the condition out of the loop, but only if it can
determine that v is not modified inside the loop. It might know this if
it can determine that all function calls inside the loop are pure, or if
v is a local auto variable that's not had its address taken. There
could be other ways it can prove this, I don't know. It's hard to
really say much in the abstract about what the compiler might or might
not do, because so much depends on the details of the code and the
information that the compiler has on hand. A testcase is much more
valuable because you can look at the -S output and see for sure.
Brian