PDA

View Full Version : Speed penalty when using RTTI?



pir
18th May 2006, 15:05
Hi!

It is said that there is a speed penalty when using RTTI. How big is it, and does this mean that most c++ programmers don't use it?

And is the penalty spread threw the whole program, at algorithms and part of the program not using polymorphic classes?

/pir

fullmetalcoder
18th May 2006, 16:37
Dunno exactly but keep in mind that using C++ brings a speed and size penalty in comparison with C; C brings a speed and size penalty in comparison with ASM

So why are we using C++ instead of ASM ?

the loss of speed and size are not relevant on our new monster computers (dual core, Gigs of RAM, hundreds of gigs of disk space...)
C++ is much safer and easier to learn and to use than ASM and still much faster than other languages such as Java, Basic, ...Thus you can consider using RTTI without even thinking about a loss of performance, unless your code is designed for a 12 years (or more) old computer...

wysota
18th May 2006, 17:18
You should first ask yourself a question where and when is rtti actually used and how to avoid it. Then it should be easy to answer that penalty question.

Brandybuck
18th May 2006, 19:30
C++ can be as fast as C, but you have to avoid a lot of the convenient abstractions to get it. But even so it isn't that much slower than C. Virtual functions will give you a tiny penalty, but for nearly every purpose it is insignificant. I've seen C++ code used for performance critical realtime systems.

RTTI is a different bag, however. You can get a significant performance hit, depending on your compiler and how you use it. Older G++ compilers used to be abysmal, but newer ones aren't too bad. Also, if you're dynamic casting or checking the typename for every parameter to every function, you're going to get a significant performance hit (as well as being a bad design).

pir
18th May 2006, 23:52
The reason I'm asking is because I'm thinking of using it in a program drawing groups of 3d objects. The reason I think I need to be able to use dynamic_cast is because I want to be able to group different kinds of obejcts into the same group and access the different types of information and other stuff that is special for that type. The usuall stuff. I think I can work around it, but it would be messy.

So I would only need to use dynamic_cast when inserting the obejcts and things like that, and that does not happen much. It is the rendering that takes all the computing time and happens when the user transforms the object. I don't need any dynamic_cast in the rendering functions.

But does the use of RTTI give me time penalty when rendering the 3d images, even if I dont use any dynamic_cast in the rendering functions?

If not, then it doesn't matter much how long it takes. But if it is, then it can be trouble. Many of the images have a lot of polygons. So I need all the computing time I can get to render smoothly when spinning the object.

wysota
19th May 2006, 01:36
If you use Qt, you can use qobject_cast instead or do your own type matching (like QCanvasItems in Qt3 do).