Does Virtual Function Impact on the efficiency of program

  • Thread starter want.to.be.professer
  • Start date
W

want.to.be.professer

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 )?
 
M

Matthias Buelow

want.to.be.professer said:
( 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.
 
J

Juha Nieminen

want.to.be.professer said:
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.
 
B

Bo Persson

want.to.be.professer said:
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
 
E

Erik Wikström

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.
 
J

Joe Smith

Erik Wikström said:
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.
 

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,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top