Does Virtual Function Impact on the efficiency of program

Discussion in 'C++' started by want.to.be.professer, Nov 21, 2008.

  1. For OO design, I like using virtual member function.But considering
    efficiency, template is better. Look at this program,

    class Animal
    {
    public:
    virtual void Walk() = 0;
    };

    class Dog
    {
    public:
    virtual void Walk()
    {
    // ...
    }
    };

    Virtual member function will make our object adding a virtual table
    pointer( size: 4 ).When using Virtual member function, computer must
    read the pointer value, and then find the virtual table.

    // use template
    template <SonClassType>
    class Animal
    {
    public:
    void Walk()
    {
    return static_cast<SonClassType*>(this)->WalkMe();
    }
    };

    class Dog : public Animal <Dog>
    {
    public:
    void WalkMe()
    {
    // ....
    }
    };

    Using template , you call the Walk(), and then WalkMe() is called, So
    It is also two times. Some books say virtual function will Impact on
    the efficiency of program, so I cannot understand( In this program,
    they both 2 times ). In this program, which is the better one
    ( efficiency )?
    want.to.be.professer, Nov 21, 2008
    #1
    1. Advertising

  2. want.to.be.professer wrote:
    > ( In this program,
    > they both 2 times ). In this program, which is the better one
    > ( efficiency )?


    The one that is shorter and clearer (which, for most people I guess,
    would be the first one.) As for any micro-optimizations, care about that
    after profiling. JFTR, I don't think your "template" version will be any
    faster since it's also doing an indirect function call but that should
    be a totally uninteresting detail.
    Matthias Buelow, Nov 21, 2008
    #2
    1. Advertising

  3. want.to.be.professer wrote:
    > For OO design, I like using virtual member function.But considering
    > efficiency, template is better.


    Calling a virtual function is slightly slower than calling a regular
    function, but not much. In most cases the difference is negligible
    (especially if the function in question performs lots of operations).

    The only situation where virtual vs. non-virtual may result in a
    considerable difference is if you need to call the function a huge
    amount of times in a tight loop, and the implementation if the function
    is itself also very short and fast. In this case overhead of the virtual
    indirection can become significant. However, it's usually rare to even
    need virtual function in such situations.
    Juha Nieminen, Nov 21, 2008
    #3
  4. want.to.be.professer

    Bo Persson Guest

    want.to.be.professer wrote:
    > For OO design, I like using virtual member function.But considering
    > efficiency, template is better. Look at this program,
    >
    > class Animal
    > {
    > public:
    > virtual void Walk() = 0;
    > };
    >
    > class Dog
    > {
    > public:
    > virtual void Walk()
    > {
    > // ...
    > }
    > };
    >
    > Virtual member function will make our object adding a virtual table
    > pointer( size: 4 ).When using Virtual member function, computer must
    > read the pointer value, and then find the virtual table.
    >
    > // use template
    > template <SonClassType>
    > class Animal
    > {
    > public:
    > void Walk()
    > {
    > return static_cast<SonClassType*>(this)->WalkMe();
    > }
    > };
    >
    > class Dog : public Animal <Dog>
    > {
    > public:
    > void WalkMe()
    > {
    > // ....
    > }
    > };
    >
    > Using template , you call the Walk(), and then WalkMe() is called,
    > So It is also two times. Some books say virtual function will
    > Impact on the efficiency of program, so I cannot understand( In
    > this program, they both 2 times ). In this program, which is the
    > better one ( efficiency )?


    The template requires that the type is known at compile time. The
    virtual function call can be resolved at run time. If you need the
    latter, a virtual function is a good choice.

    If you don't need it, don't use it. :)


    Bo Persson
    Bo Persson, Nov 21, 2008
    #4
  5. On 2008-11-21 15:23, want.to.be.professer wrote:
    > For OO design, I like using virtual member function.But considering
    > efficiency, template is better. Look at this program,


    > Virtual member function will make our object adding a virtual table
    > pointer( size: 4 ).When using Virtual member function, computer must
    > read the pointer value, and then find the virtual table.


    > Using template , you call the Walk(), and then WalkMe() is called, So
    > It is also two times. Some books say virtual function will Impact on
    > the efficiency of program, so I cannot understand( In this program,
    > they both 2 times ). In this program, which is the better one
    > ( efficiency )?


    Virtual functions and templates serves different purposes, you only need
    virtual functions when you do not know the exact type while your are
    writing the code (i.e. the type will be determined at run-time), while
    templates are used when you do know the exact type at compile-time.

    Virtual functions do come with a slight penalty but in most cases it is
    negligible. A rule of thumb is to not make functions virtual unless you
    have to (i.e. you need to override them in a derived class) but don't be
    afraid to do so when you have to.

    --
    Erik Wikström
    Erik Wikström, Nov 21, 2008
    #5
  6. want.to.be.professer

    Joe Smith Guest

    "Erik Wikström" <> wrote:
    >
    > Virtual functions do come with a slight penalty but in most cases it is
    > negligible. A rule of thumb is to not make functions virtual unless you
    > have to (i.e. you need to override them in a derived class) but don't be
    > afraid to do so when you have to.
    >

    Also all decent C++ compilers including the earliest versions of cfront will
    optimize away the overhead of calling a vitrual function when possible, such
    as
    when you are calling the meber from an actual object rather than a
    base-class pointer/reference.
    Joe Smith, Nov 21, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave

    Search Engine Impact of ASP

    Dave, Jul 15, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    343
  2. Scott Cadillac
    Replies:
    0
    Views:
    336
    Scott Cadillac
    Oct 25, 2003
  3. Sky Sigal
    Replies:
    0
    Views:
    373
    Sky Sigal
    Jul 31, 2004
  4. IK
    Replies:
    2
    Views:
    590
    hemraj
    Jul 23, 2004
  5. Replies:
    1
    Views:
    414
    Markus Schoder
    Apr 30, 2007
Loading...

Share This Page