Results 1 to 6 of 6

Thread: Speed penalty when using RTTI?

  1. #1
    Join Date
    May 2006
    Location
    Stockholm, Sweden
    Posts
    81
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Speed penalty when using RTTI?

    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

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Speed penalty when using RTTI?

    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...
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Speed penalty when using RTTI?

    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.

  4. #4
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Speed penalty when using RTTI?

    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).

  5. #5
    Join Date
    May 2006
    Location
    Stockholm, Sweden
    Posts
    81
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Speed penalty when using RTTI?

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Speed penalty when using RTTI?

    If you use Qt, you can use qobject_cast instead or do your own type matching (like QCanvasItems in Qt3 do).

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.