| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"George2" |
| Date: |
31 Jan 2008 03:08:14 AM |
| Object: |
Performance and footprint of virtual function |
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;}
};
[/Code]
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
.
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: Performance and footprint of virtual function |
31 Jan 2008 03:17:12 AM |
|
|
* 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;}
};
[/Code]
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
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
|
|
|
|

|
Related Articles |
|
|