Does Virtual Function Impact on the efficiency of program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • want.to.be.professer

    Does Virtual Function Impact on the efficiency of program

    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<Son ClassType*>(thi s)->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 )?
  • Matthias Buelow

    #2
    Re: Does Virtual Function Impact on the efficiency of program

    want.to.be.prof esser 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.

    Comment

    • Juha Nieminen

      #3
      Re: Does Virtual Function Impact on the efficiency of program

      want.to.be.prof esser 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.

      Comment

      • Bo Persson

        #4
        Re: Does Virtual Function Impact on the efficiency of program

        want.to.be.prof esser 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<Son ClassType*>(thi s)->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



        Comment

        • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

          #5
          Re: Does Virtual Function Impact on the efficiency of program

          On 2008-11-21 15:23, want.to.be.prof esser 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

          Comment

          • Joe Smith

            #6
            Re: Does Virtual Function Impact on the efficiency of program


            "Erik Wikström" <Erik-wikstrom@telia. comwrote:
            >
            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.

            Comment

            Working...