By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

Does Virtual Function Impact on the efficiency of program

P: n/a
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 )?
Nov 21 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
Nov 21 '08 #2

P: n/a
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.
Nov 21 '08 #3

P: n/a
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

Nov 21 '08 #4

P: n/a
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
Nov 21 '08 #5

P: n/a

"Erik Wikström" <Er***********@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.

Nov 21 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.