Connecting Tech Pros Worldwide Forums | Help | Site Map

Does Virtual Function Impact on the efficiency of program

want.to.be.professer
Guest
 
Posts: n/a
#1: Nov 21 '08
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 )?

Matthias Buelow
Guest
 
Posts: n/a
#2: Nov 21 '08

re: Does Virtual Function Impact on the efficiency of program


want.to.be.professer wrote:
Quote:
( 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.
Juha Nieminen
Guest
 
Posts: n/a
#3: Nov 21 '08

re: Does Virtual Function Impact on the efficiency of program


want.to.be.professer wrote:
Quote:
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.
Bo Persson
Guest
 
Posts: n/a
#4: Nov 21 '08

re: Does Virtual Function Impact on the efficiency of program


want.to.be.professer wrote:
Quote:
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



=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
#5: Nov 21 '08

re: Does Virtual Function Impact on the efficiency of program


On 2008-11-21 15:23, want.to.be.professer wrote:
Quote:
For OO design, I like using virtual member function.But considering
efficiency, template is better. Look at this program,
Quote:
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.
Quote:
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
Joe Smith
Guest
 
Posts: n/a
#6: Nov 21 '08

re: Does Virtual Function Impact on the efficiency of program



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

Closed Thread


Similar C / C++ bytes