 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
klaus Guest
|
Posted: Fri Aug 29, 2003 8:25 am Post subject: For loop compilation optimization |
|
|
Hello
Consider the following code:
for(int i=0; i < something.GetNoOfSomething(); i++)
{
something.DoSomething();
}
I am wondering how the compiler optimizes or doesn't optimize the above
code.
Is the something.GetNoOfSomething() function executed every time the for
loop takes a round?
I suppose the compiler would have to consider if the something.DoSomething()
would affect the result of something.GetNoOfSomething().
Thanks
Klaus
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Fri Aug 29, 2003 3:43 pm Post subject: Re: For loop compilation optimization |
|
|
On Fri, 29 Aug 2003 04:25:37 -0400, klaus wrote:
| Quote: | Hello
Consider the following code:
for(int i=0; i < something.GetNoOfSomething(); i++)
{
something.DoSomething();
}
I am wondering how the compiler optimizes or doesn't optimize the above
code.
Is the something.GetNoOfSomething() function executed every time the for
loop takes a round?
I suppose the compiler would have to consider if the something.DoSomething()
would affect the result of something.GetNoOfSomething().
|
If the function returns some constant member variable, or a reference,
then a good optimizer would inline that call probably, but you should not
rely on this.
Regards,
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joshua Lehrer Guest
|
Posted: Sat Aug 30, 2003 7:35 pm Post subject: Re: For loop compilation optimization |
|
|
"Nils Petter Vaskinn" <no (AT) spam (DOT) for.me.invalid> wrote
| Quote: | If you as the programmer KNOW that GetNoOfSomething can be safely
optimized away you should do it by hand to make certain:
int tmp = something.GetNoOfSomething();
for(int i=0; i < tmp; ++i) {
do_something();
}
|
and indicate such using proper language constructs:
CONST int no_of_something(something.GetNoOfSomething());
for (int i=0;i
do_something();
}
joshua lehrer
factset research systems
NYSE:FDS
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sean Fraley Guest
|
Posted: Wed Sep 03, 2003 12:21 am Post subject: Re: For loop compilation optimization |
|
|
Andrei Alexandrescu wrote:
| Quote: | That's irrelevant. It also leads in the false direction that the optimizer
works on a program representation that's close to source language. In
reality, most optimizations occur having the control and data flow graphs
handy, after inlining has been carried on, and on a simple intermediary
language.
To answer the OP's question: the optimization you mention is a standard
one, called "loop-invariant code motion". In practice, it can be performed
if (1) the optimizer implements this optimization, (2) if all calls inside
the for loop are inlined (3) if the alias analysis has shown that the loop
body can't in any way affect the vector's size. (3) is really tricky in
C++, so you're better off precomputing by hand.
Andrei
|
Thank you for the clarification. I've not studied compiler design that much
yet.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|