Performance and footprint of virtual function

G

George2

Hello everyone,


I want to know why virtual function's footprint and performance is
compared with normal function call.

Here is my analysis, please review whether I am correct. Thanks.

1. foorprint

Code:
class Foo
{
protected:
		char * pName;
		int nSize;
		virtual void copyName(char* pN) {return;}
		void virFunc() {return;}
};

Class Foo instance will consume 12 bytes other than 8 bytes to hold an
additonal __vfptr pointing to the entry address of virtual function
table. If we remove virtual keyword, the footprint of class Foo
instance will consume only 8 bytes.

2. performance

2.1 Virtual function call's performance is worse than normal function
call is because it needs to use __vfptr to find the virtual function
table, then invokes the function. Here using __vfptr brings one more
level of indirection compared with normal function call, which is the
most important reason making the performance worse.

2.2 Are there anything else matters performance?
2.3 How to find normal function call address can be determined during
compile time and virtual function call address can only be determined
during runtime?

Please comment whether my understanding of 1 and 2 are correct or not?


thanks in advance,
George
 
A

Alf P. Steinbach

* George2:
Hello everyone,


I want to know why virtual function's footprint and performance is
compared with normal function call.

Here is my analysis, please review whether I am correct. Thanks.

1. foorprint

Code:
class Foo
{
protected:
		char * pName;
		int nSize;
		virtual void copyName(char* pN) {return;}
		void virFunc() {return;}
};

Class Foo instance will consume 12 bytes other than 8 bytes to hold an
additonal __vfptr pointing to the entry address of virtual function
table. If we remove virtual keyword, the footprint of class Foo
instance will consume only 8 bytes.

2. performance

2.1 Virtual function call's performance is worse than normal function
call is because it needs to use __vfptr to find the virtual function
table, then invokes the function. Here using __vfptr brings one more
level of indirection compared with normal function call, which is the
most important reason making the performance worse.

2.2 Are there anything else matters performance?
2.3 How to find normal function call address can be determined during
compile time and virtual function call address can only be determined
during runtime?

Please comment whether my understanding of 1 and 2 are correct or not?

It's incorrect.

Please let us know whether you're reading this newsgroup.


- Alf
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top