473,748 Members | 10,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RTTI question

I know than dynamic_cast check string name of derived to base class and
if one of them match, return the pointer of that object or else zero.

I suppose, I dynamic_cast instead of strings, checks integers, then this
procedure will be more fast, so I create something like this:

---------------------------------------------
class A
{
public:
const static int RTTI = 0;
virtual bool checkRTTI(int rtti) { return RTTI == rtti; }
};

class B : public A
{
public:
const static int RTTI = 1;
virtual bool checkRTTI(int rtti) { return RTTI == rtti ||
A::checkRTTI(rt ti); }
};

int main()
{
B b;
A *a = &b;
a->checkRTTI(B::R TTI);
return 0;
}
---------------------------------------------

I am curius why when I count the time to do this:
a->checkRTTI(B::R TTI)
I found it 5 times bigger than this:
dynamic_cast<B* >(a) ? true : false

I am offtopic? Compiler is mingw-g++

thanks!
Jan 3 '07 #1
2 2246
Chameleon wrote:
>
I am curius why when I count the time to do this:
a->checkRTTI(B::R TTI)
I found it 5 times bigger than this:
dynamic_cast<B* >(a) ? true : false

I am offtopic? Compiler is mingw-g++
It probably is off topic...your answer for implementation
details is probably best asked in a group specific to
your compiler.

However, I suspect the major reason is that dynamic_cast
is very nicely inlined as the compiler knows just exactly
what you are doing. The "B*" is effectively constant
as the cast operand.

Virtual calls are (near) impossible to inline. It has
to emit code to pass the value B::RTTI and invoke the
virtual call.
Jan 3 '07 #2

Chameleon wrote:
I know than dynamic_cast check string name of derived to base class and
if one of them match, return the pointer of that object or else zero.

I suppose, I dynamic_cast instead of strings, checks integers, then this
procedure will be more fast, so I create something like this:

---------------------------------------------
class A
{
public:
const static int RTTI = 0;
virtual bool checkRTTI(int rtti) { return RTTI == rtti; }
};

class B : public A
{
public:
const static int RTTI = 1;
virtual bool checkRTTI(int rtti) { return RTTI == rtti ||
A::checkRTTI(rt ti); }
};

int main()
{
B b;
A *a = &b;
a->checkRTTI(B::R TTI);
return 0;
}
---------------------------------------------

I am curius why when I count the time to do this:
a->checkRTTI(B::R TTI)
I found it 5 times bigger than this:
dynamic_cast<B* >(a) ? true : false

I am offtopic? Compiler is mingw-g++

thanks!
My understanding (without actually looking it up) is the standard does
not specify how a compiler is to implement dynamic_cast, just that if
you use dynamic_cast it should be on classes with at least one virtual
function. I surmise that strings are chosen 1) because class-to-string
already exists in the form of name mangling, so why repeat yourself?
and 2) That mapping a type to an integer is difficult at best when
using dynamically loaded modules, which doesnt even have to be compiled
by the same compiler.

COM implements a thing nearly the same as dynamic_cast, namely
QueryInterface, that does use integers. But if you really wanted a fast
lookup of dynamic_cast (which in many exception implementation is used
internally by the compiler to match an exception with a catch handler)
then there are hash tables that offer very fast lookups. Look at
implementations of exception handling --the Wiki page is a good
starting point.

Why is your example so much slower? Because the compiler already knows
at compiler time that a is really a B* , it is smart enough to follow
what you did and optimize. If a was initialized from a value that comes
from a different translation unit, then your results will vary.

But this goes to show that you can rarely optimize things without
actually running a profiler on it -- results are rarley if ever what
you expect.

Jan 3 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2046
by: Rick | last post by:
Hi, I wrote a few classes recently and am writing a small GC implementation for C++. I will be implementing my own gc_ptr type maintain a list where I'll store pointers to allocated memory. I was wondering if it is possible to identify objects during runtime via RTTI because I don't want to be doing: gc_ptr<int>::doGC(); gc_ptr<double>::doGC();
2
5978
by: shishir | last post by:
Please consider the following //file test.cpp #include <iostream> int main() { int x; try { throw x;
6
1946
by: Kleidemos | last post by:
If I implement a simple RTTI system, more simple than C++ RTTI system for my program and this system is plus or minus: #define DEF_RTTI_BASE(name) virtual inline const char *Name(){ return #name; } #define DEF_RTTI(name) inline const char *Name(){ return #name; } And(for example) class CProva
9
4628
by: Agoston Bejo | last post by:
Hello there, I would like to know what overheads there are to think of when using RTTI. I understand that enabling RTTI increases the sizes of the classes, but not the objects themselves. This doesn't sound too bad. What I'm worried about is whether there is a general performance overhead caused by enabling RTTI, such as enabling exceptions makes a C++ program slower even when there are no exceptions actually used. Are there similar...
2
3778
by: denny | last post by:
Hey all, I know that dynamic_cast<> takes some time, but , for instance, is there a memoy cost associated in with it? Does it have to maintain a table in memory, thus bloating the runtime ram needs of my dll? Does it bloat the actual download size - would my dll be smaller without it? thanks - I'm using rtti in some instances, but I jsut want to know if it's costing me hundreds of K in extra download. -denny-
3
491
by: Neo | last post by:
Hi Friends, I have a question about RTTI. 1) In C++ we have vptr pointing to vtables which helps to make use of pointer as polymorphic entities. 2) Without knowing object type I can call methods of object using those pointers. So in which case RTTI is needed? ( Why I need to know my object type?) Regards,
5
3023
by: dotNeter | last post by:
I'm studying the RTTI, and my current work is concern for how to get the self-defined type at runtime, that's exactly what the RTTI does. I mean, in my application, I built several self-defined data types, so I have to implement the RTTI by myself. I need a simple and effective example to help me decide how to design. Can someon help me?
33
3570
by: mscava | last post by:
Well I've got a problem, that is more theoretical than practital. I need to know benefits of RTTI. I see another way of doing it... class A { public: ~virtual A() {} enum Type { X, Y, Z }; Type GetType() { return type_; }
3
2040
by: Chameleon | last post by:
What is better if you want upcasting in intermediate classes like below? Multiple Inheritance and Overloading or simply RTTI? RTTI wants time but MI and Overloading create big objects (because of virtual) and finally they need time too to access objects inside big object. (Examples looks complicated but are very simple) Paradigm #1: MI & Overloading =============================
0
8831
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,...
0
9548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
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();...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.