is const function faster than non-const?

Y

Yuming Ma

consider this:

class X
{
void foo() const;
void goo();
}

suppose that foo() and goo() has exactly the same content, is foo() faster
than goo()? what does compiler optomization does to const function?

thanks,

yuming,
 
J

Jack Klein

consider this:

class X
{
void foo() const;
void goo();
}

suppose that foo() and goo() has exactly the same content, is foo() faster
than goo()?

Yes or no.
what does compiler optomization does to const function?

Ask in a newsgroup that supports your compiler, or run timing tests.
The standard does not specify how long anything takes, nor does it
define compiler optimizations. It is all compiler-specific.
thanks,

yuming,

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
D

Dimitris Kamenopoulos

Yuming said:
consider this:

class X
{
void foo() const;
void goo();
}

suppose that foo() and goo() has exactly the same content, is foo() faster
than goo()? what does compiler optomization does to const function?

I shouldn't think the whole "const" business makes much of a difference in
performance. const is supposed to help the programmer write more correct
code by enabling a series of static const-correctness checks. If there are
optimizations that can be enabled by const, they certainly are compiler and
hardware specific. Why don't you try profiling your example to see if there
is any difference between foo and goo?
 
P

Peter van Merkerk

Yuming said:
consider this:

class X
{
void foo() const;
void goo();
}

suppose that foo() and goo() has exactly the same content, is foo()
faster than goo()? what does compiler optomization does to const
function?

Often the best way to answer the "which is faster" type of question is to
try both ways and measure which one is the fastest. However it is unlikely
'const' will make a difference. The main purpose of const is helping you to
write correct programs. I recommend you use const whenever possible .
 
J

jeffc

Yuming Ma said:
consider this:

class X
{
void foo() const;
void goo();
}

suppose that foo() and goo() has exactly the same content, is foo() faster
than goo()? what does compiler optomization does to const function?

I can't imagine anything it might do.
 
W

wogston

class X
{
void foo() const;
void goo();
}

suppose that foo() and goo() has exactly the same content, is foo() faster
than goo()? what does compiler optomization does to const function?

Not very likely, the foo() is possible to call for const objects of type X,
that's the primary purpose of it in this context. It also makes the object
const for duration of the method call, so you cannot write to members or
call non-const methods, so it's more of static compilation-time const
enforcement tool than anything else.

The primary use (in practise, personal observation only) is to allow to
query state from const objects, along the lines of:

void bar(const object& x)
{
int v = x.foo();
...

... in this light having a const method, foo(), like you have above without
return value or arguments is a bit useless, unless the method does something
with static member, global variable or function or something similiar. But
it will have very little to do with the current object, it can't really do
anything to it without mutable members in the object (or some other cases
that don't spring to mind immediately :)

... useless answer? I think so, too, but that's the thoughts that passed
through my mind as of few minutes ago. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top