is const function faster than non-const?

Discussion in 'C++' started by Yuming Ma, Dec 8, 2003.

  1. Yuming Ma

    Yuming Ma Guest

    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,
     
    Yuming Ma, Dec 8, 2003
    #1
    1. Advertisements

  2. Yuming Ma

    Jack Klein Guest

    Yes or no.
    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.
    --
    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
     
    Jack Klein, Dec 8, 2003
    #2
    1. Advertisements

  3. 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?
     
    Dimitris Kamenopoulos, Dec 8, 2003
    #3
  4. 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, Dec 8, 2003
    #4
  5. Yuming Ma

    jeffc Guest

    I can't imagine anything it might do.
     
    jeffc, Dec 8, 2003
    #5
  6. Yuming Ma

    wogston Guest

    class X
    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. :)
     
    wogston, Dec 8, 2003
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.