C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

For loop compilation optimization

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
klaus
Guest





PostPosted: Mon Aug 25, 2003 10:25 pm    Post subject: For loop compilation optimization Reply with 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().


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
Jeff Greif
Guest





PostPosted: Tue Aug 26, 2003 8:26 am    Post subject: Re: For loop compilation optimization Reply with quote



Besides the reason you mention, most compilers cannot take a chance on
optimizing this.
-- Unless getNoOfSomething looks like
int GetNoOfSomething() const
the compiler cannot tell that it doesn't modify something.
-- Without analyzing the body of GetNoOfSomething, it cannot tell whether
it returns the same thing on successive calls if the object 'something' has
not changed. For example, it could return the number of nanoseconds since
the Big Bang.
-- The 'something' container could be modified in another thread, so the
compiler cannot optimize unless the compiler can know that no other threads
will run (owing to some compiler setting whose semantics it understands and
which will be enforced at runtime).

There a probably some other possible complications also.

Jeff

"klaus" <kh (AT) slet (DOT) ingenioer.dk> 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().


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
llewelly
Guest





PostPosted: Tue Aug 26, 2003 7:08 pm    Post subject: Re: For loop compilation optimization Reply with quote



"klaus" <kh (AT) slet (DOT) ingenioer.dk> writes:

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?
[snip]


In general, if GetNoOfSomething() gets inlined, the inlined code may
get hoisted out of the loop. If it doesn't get inlined, it almost
certainly won't; I don't know of any compilers which attempt to
analyze non-inlined functions for side-effects.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David Leimbach
Guest





PostPosted: Wed Aug 27, 2003 8:58 am    Post subject: Re: For loop compilation optimization Reply with quote

[email]ryan.winter (AT) optusnet (DOT) com.au[/email] (Ryan Winter) wrote in message news:<5dadf782.0308251700.5079c233 (AT) posting (DOT) google.com>...
Quote:
"klaus" <kh (AT) slet (DOT) ingenioer.dk> wrote

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().

You are correct. If you defined the something.DoSomething() as const,
then the compiler would know that it could easily optimise the
getNoOfSomething to be called only once. Otherwise it has no way to
tell.

Actually const member fuctions can still modify "mutable" member data can't they?

In this case you cannot annotate a member function such that the state of even a
const object won't be modified.

#include class A
{
mutable int x;
public:
A (A const & a) : x(a.x) {}
A () : x() {}
void blah () const
{
++x;
}
int get () const
{
return x;
}
};

int main ()
{
const A a;
for (int i = 0; i < 101; ++i)
a.blah();
std::cout << a.get() << std::endl;
}

Quote:

You would be much better of calling GetNoOfSomething() before the
loop.

Yes Smile.

But one must be careful with this rule:

for (m_iterator = m.begin(); m_iterator != m.end(); ++m_itereator)
{
//some stuff that might make the data structure grow
//or invalidate your iterators BE CAREFUL!
}

Sometimes you actually need to constantly query the object for the state
of where a pointer/iterator may lead.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jeff Greif
Guest





PostPosted: Wed Aug 27, 2003 9:50 am    Post subject: Re: For loop compilation optimization Reply with quote


"Ben Hutchings" <do-not-spam-benh (AT) bwsint (DOT) com> wrote

Quote:
In article <e2y2b.97512$2x.28629 (AT) rwcrnsc52 (DOT) ops.asp.att.net>,
Jeff Greif wrote:
.....
-- The 'something' container could be modified in another thread, so
the
compiler cannot optimize unless the compiler can know that no other
threads
will run (owing to some compiler setting whose semantics it understands
and
which will be enforced at runtime).

Wrong. The compiler is not required to synchronise access by different
threads; that's the responsibility of the programmer.

I was not referring to synchronization issues that are the responsibility of
the programmer. What I meant was that if the compiler were invoked with
options like
--singleThreaded --noSharedMemory --singleThreadedLibraries
and these options guaranteed that there could be no other access to
'something' for the duration of the loop, the compiler would be allowed to
take advantage of this information to optimize the loop.

Jeff


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed Aug 27, 2003 3:47 pm    Post subject: Re: For loop compilation optimization Reply with quote

Ben Hutchings <do-not-spam-benh (AT) bwsint (DOT) com> wrote

Quote:
In article <e2y2b.97512$2x.28629 (AT) rwcrnsc52 (DOT) ops.asp.att.net>,
Jeff Greif wrote:
Besides the reason you mention, most compilers cannot take a chance
on optimizing this.
-- Unless getNoOfSomething looks like
int GetNoOfSomething() const
the compiler cannot tell that it doesn't modify something.

A const member function can still modify static data and data
reachable through static pointers.

A const member function could also modify mutable data. For that
matter, a const member function can use const_cast to modify anything it
pleases, as long as the original object is not const.

Basically, const means nothing to the optimizer.

Also, of course, the GetNoOfSomething() function may have observable
side effects, writing to a file, for example, and these must occur.

With regards to the original question, the answer is simple: the
compiler can do anything it wishes, as long as the observable behavior
(output, access to volatile variables) is the same as if it had called
the function each time through the loop. In practice, of course, unless
the function is inline and trivial, there are very, very few optimizers
which will be capable of suppressing the call each time through the
loop.

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Kevin Cline
Guest





PostPosted: Sat Aug 30, 2003 1:17 pm    Post subject: Re: For loop compilation optimization Reply with quote

[email]ryan.winter (AT) optusnet (DOT) com.au[/email] (Ryan Winter) wrote in message news:<5dadf782.0308251700.5079c233 (AT) posting (DOT) google.com>...
Quote:
"klaus" <kh (AT) slet (DOT) ingenioer.dk> wrote

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().

You are correct. If you defined the something.DoSomething() as const,
then the compiler would know that it could easily optimise the
getNoOfSomething to be called only once. Otherwise it has no way to
tell.

You would be much better of calling GetNoOfSomething() before the
loop.

Maybe not. Doing that makes the code just a bit harder to read. It
might be better to make GetNoOfSomething() an inline function. Or to
redesign the interface so that you don't have to write all those
infernal loops:

something.ForEachSomething(myFunction);

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.