31

For my GUI API which works with a variety of backends (sdl, gl, d3d, etc) I want to dynamically cast the generic type image to whatever it may happen to be.

So the bottom line is, I would be doing around 20 * 60fps dynamic casts per second.

How expensive is a dynamic cast? Will I notice that it has a noticeable negative impact on performance? What alternatives do I have that still maintain an acceptable level of performance?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • 3
    Hard to tell. Why don't you try it and measure the performance? – Fred Foo Apr 17 '11 at 15:09
  • 2
    Maybe it was so in earlier days, but now with all the advancemnts in processing speeds and compiler developments very hard to know without trying it out. – DumbCoder Apr 17 '11 at 15:18
  • A simple SO search reveals some interesting answers (not exact duplicates, but pretty close): http://stackoverflow.com/questions/28080/how-bad-is-dynamic-casting and http://stackoverflow.com/questions/579887/how-expensive-is-rtti – SoapBox Apr 17 '11 at 15:21
  • 1
    Some other programming languages do dynamic casting very heavily, and yet they manage to do millions of such operations per second. I'm sure that any C++ implementations should be able to do 1200/s! – Donal Fellows Apr 17 '11 at 16:26
  • FYI: http://tinodidriksen.com/2010/04/14/cpp-dynamic-cast-performance/, IMO you should reconsider your design, to avoid dynamic_casts, as your current design violates quite many principles of good software architecture (e.g. http://en.wikipedia.org/wiki/Open/closed_principle ). – smerlin Apr 17 '11 at 18:11

3 Answers3

27

1200 dynamic_casts per second is not likely to be a major performance problem. Are you doing one dynamic_cast per image, or a whole sequence of if statements until you find the actual type?

If you're worried about performance, the fastest ways to implement polymorphism are:

  • --- fastest ---
  • Function overloading (compile-time polymorphism only)
  • CRTP (compile-time polymorphism only)
  • Tags, switches and static casts (brittle, doesn't support multi-level inheritance, a maintenance headache so not recommended for unstable code)
  • Virtual functions
  • Visitor pattern (inverted virtual function)
  • --- almost as fast ---

In your situation, the visitor pattern is probably the best choice. It's two virtual calls instead of one, but allows you to keep the algorithm implementation separate from the image data structure.

FRob
  • 3,883
  • 2
  • 27
  • 40
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
-1

Can't you define your own cast using a #define, which uses dynamic_cast in debug build (so you know your cast is correct) and does a simple (MySubclass *) cast in release build so there is no performance cost?

Joris Mans
  • 6,024
  • 6
  • 42
  • 69
  • 1
    `dynamic_cast` returns null if the pointer cannot be converted. It is likely that the original program depends on this behavior. – dfan Apr 17 '11 at 15:19
  • @dfan or throws if you are casting references –  Apr 17 '11 at 15:38
  • 2
    `static_cast` does not perform cross-casts, even when the `dynamic_cast` would succeed. – Ben Voigt Apr 17 '11 at 15:58
-1

In this particular circumstance, you should be able to organise your code so that the dynamic_cast is only needed once. I imagine that the backend in not changing dynamically.

zdan
  • 28,667
  • 7
  • 60
  • 71