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 -
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
4 2441
1. is correct.
2.1 is silly. The virtual function is called by:
obj->vtblptr->func
Using a function pointer is;
fptr->funct
Using a funciton pointer argument to call a function:
ptr ->copy as arg->call func
So you can say that virtual functions are worse than a normal function call becuse of the added layer of indirection. Please note this applies to ALL function arguments that are pointers since they have to be copied and dereferenced later.
The most you can say is using a pointer is slower than not using a pointer.
The icing on this cake is that ALL functions are called by address (i.e. a pointer).
2.3 is incorrect. All function addresses are known at compile time. Face it, if they are unknown then you can't link.
Thanks for your great reply, weaknessforcats !
1.
Using a function pointer is;
fptr->funct
Using a funciton pointer argument to call a function:
ptr ->copy as arg->call func
I do not quite understand what is the type of fptr and funct? What do you mean "ptr ->copy as arg->call func"? Could you show some simple pseudo code please?
2.
2.3 is incorrect. All function addresses are known at compile time. Face it, if they are unknown then you can't link.
Do not agree. The type can not be determined during compile time, so compile could only pass pointer to the base address of virtual function table. Compiler can not find the exact function to be invoked during compile time (since it is virtual).
regards,
George
Georege2, take a deep breath and relax.
I do not quite understand what is the type of fptr and funct? What do you mean "ptr ->copy as arg->call func"? Could you show some simple pseudo code please?
What I meant to convey was that all functions are called by address. You always use a function pointer to call a funcion. That is, the name of a function is the address of the function. To call, you just jump to that address.
If you use a function pointer variable, you just jump th the address in the variable.
It's a single jump.
Overhead is 0.
Do not agree. The type can not be determined during compile time, so compile could only pass pointer to the base address of virtual function table. Compiler can not find the exact function to be invoked during compile time (since it is virtual).
You don't know what you are talking about. The virtual function table contains the addresses of the class virtual functions, which are known at compile time by the compiler. Otherwise, the virtual table couldn't be built.
When you call a virtual function, the compiler has inserted code to access the virtual funciton table to a particular offset for that function and just jumps to the function at that address.
It's a simple function pointer operation.
BTW: No types can be discovered at run time. All you have are the compiled bits.
Thanks weaknessforcats ,
I have understood your points now. My question is answered.
Georege2, take a deep breath and relax.
What I meant to convey was that all functions are called by address. You always use a function pointer to call a funcion. That is, the name of a function is the address of the function. To call, you just jump to that address.
If you use a function pointer variable, you just jump th the address in the variable.
It's a single jump.
Overhead is 0.
You don't know what you are talking about. The virtual function table contains the addresses of the class virtual functions, which are known at compile time by the compiler. Otherwise, the virtual table couldn't be built.
When you call a virtual function, the compiler has inserted code to access the virtual funciton table to a particular offset for that function and just jumps to the function at that address.
It's a simple function pointer operation.
BTW: No types can be discovered at run time. All you have are the compiled bits.
regards,
George
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Fred |
last post by:
Has anyone a link or any information comparing c and c++ as far as
execution speed is concerned?
Signal Processing algorithms would be welcome...
Thanks
Fred
|
by: christopher diggins |
last post by:
Since nobody responded to my earlier post , I thought I
would try to explain what I am doing a bit differently.
When multiply inheriting pure virtual (abstract) base classes, a class
obviously bloats quickly for each new vtable needed. Execution slows down
considerably as well. You can work around this by using interfaces
referemnces which have a pointer to the object and a pointer to an external
function lookup table. This technique...
|
by: D |
last post by:
Hello I'm learning C++ and having done some Asm and C.
I would like input on the following. If I understand what I'm
reading then Objects of a type of class gets a copy of it's class'
(including inherited classes) data members in memory for each
instance of an object.
But all the code, of objects of a type of class
(including inherited classes) is just one copy in memory to be
shared by all the Objects of that type of class.
Even with...
|
by: bjarne |
last post by:
Willy Denoyette wrote;
> ... it
> was not the intention of StrousTrup to the achieve the level of efficiency
> of C when he invented C++, ...
Ahmmm. It was my aim to match the performance of C and I achieved that
aim very early on. See, for example "The Design and Evolution of C++".
-- Bjarne Stroustrup; http://www.research.att.com/~bs
|
by: tman |
last post by:
I am generating a very large tree list in my program and while it's
performance is great once loaded it takes a really long time to load.
I create a root TreeNode "offline" and go through the process of
creating building up the tree from there. Only when I am done do I go
over to the actual TreeView object on my form and add the my root to
the TreeView. This sinlge step when I add my constructed root node to
the form's TreeView control...
| |
by: Michael D. Ober |
last post by:
When calling Enqueue, the internal array may need to be reallocated. My
question is by how much? In the old MFC array classes, you could tell MFC
how many additional elements to add to the array when it needed to
reallocate, which greatly boosted performance relative to adding 1 element
at a time.
Thanks,
Mike Ober.
|
by: toton |
last post by:
Hi,
I want a few of my class to overload from a base class, where the
base class contains common functionality. This is to avoid repetition
of code, and may be reducing amount of code in binary, not to get
polymorphic behavior.
None of them has virtual methods, and are self contained (no
destructor at all) thus do not have a chance to have memory error.
Thus the derived classes has additional functionality, not additional
data.
|
by: atlaste |
last post by:
Hi,
I'm currently developing an application that uses a lot of
computational power, disk access and memory caching (to be more exact:
an information retrieval platform). In these kind of applications the
last thing that remains is bare performance tuning.
So for example, you can do an 'if then else' on a bit like a 'case/
switch', an 'if/then/else' and as a multiplication with a static
buffer. Or, you can do sorting with an inline...
|
by: developer28 |
last post by:
Hi,
I am looking for some performance monitoring tools and was suggested
to ask you for the same.
Can anyone please provide some help on the same?
What i basically want is a tool that can answer some questions like:
Which of the following loops is faster?
for (i=0; i<10000; i++)
for (j=0; j<1000; j++)
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |