473,473 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

is const function faster than non-const?

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,

Jul 22 '05 #1
5 1971
On Mon, 8 Dec 2003 01:02:43 -0500, Yuming Ma <ma*******@yahoo.com>
wrote in comp.lang.c++:
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
Jul 22 '05 #2
Yuming Ma wrote:
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?
Jul 22 '05 #3
Yuming Ma wrote:
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 .

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #4

"Yuming Ma" <ma*******@yahoo.com> wrote in message
news:Pi*****************************@home2.local.. .
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.
Jul 22 '05 #5
> 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. :)
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Nickolay Kolev | last post by:
Hi all, I am currently writing some simple functions in the process of learning Python. I have a task where the program has to read in a text file and display some statistics about the tokens in...
17
by: Nathan Given | last post by:
Hello All, I am trying to debug a broken query. The query uses Left$(,4) instead of Left(,4). What is the difference between the Left() and Left$() functions in Microsoft Access? Thanks!...
109
by: Neo | last post by:
Hi Folks,http://www.abarnett.demon.co.uk/tutorial.html#FASTFOR Page states:for( i=0; i<10; i++){ ... }i loops through the values 0,1,2,3,4,5,6,7,8,9 If you don't care about the order of the loop...
27
by: Luke Wu | last post by:
Is there a C function that returns the number of digits in an input int/long? example: numdigits(123) returns 3 numdigits(1232132) returns 7
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
7
by: Bryan Parkoff | last post by:
C/C++ Compiler encourages to limit 1,000 functions under function pointer, but I am allowed to overlimit 4,096 functions. I am just for fun to test how it works. The problem is that source code...
10
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
13
by: jacklisp | last post by:
When I do the exercises I met a question: In a two's complement number system, x &= (x-1) deletes the rightmost 1-bit in x. Explain why. Use this observation to write a faster version of bitcount....
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.