473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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(rtti); }
};

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

I am curius why when I count the time to do this:
a->checkRTTI(B::RTTI)
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 2228
Chameleon wrote:
>
I am curius why when I count the time to do this:
a->checkRTTI(B::RTTI)
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(rtti); }
};

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

I am curius why when I count the time to do this:
a->checkRTTI(B::RTTI)
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
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...
2
by: shishir | last post by:
Please consider the following //file test.cpp #include <iostream> int main() { int x; try { throw x;
6
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...
9
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...
2
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...
3
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...
5
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...
33
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 }; ...
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
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,...

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.