Performance of Dynamic_Cast

Performance of dynamic_cast?

Firstly, you need to measure the performance over a lot more than just a few iterations, as your results will be dominated by the resolution of the timer. Try e.g. 1 million+, in order to build up a representative picture. Also, this result is meaningless unless you compare it against something, i.e. doing the equivalent but without the dynamic casting.

Secondly, you need to ensure the compiler isn't giving you false results by optimising away multiple dynamic casts on the same pointer (so use a loop, but use a different input pointer each time).

Dynamic casting will be slower, because it needs to access the RTTI (run-time type information) table for the object, and check that the cast is valid. Then, in order to use it properly, you will need to add error-handling code that checks whether the returned pointer is NULL. All of this takes up cycles.

I know you didn't want to talk about this, but "a design where dynamic_cast is used a lot" is probably an indicator that you're doing something wrong...

How expensive are dynamic casts in C++?

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.

dynamic_cast overhead in C++

Did you profile it?

The rule is:

  • Use static_cast when you know that the target type is valid.
  • Use dynamic_cast when you're not sure, and you need the program to look up the object's runtime type for you.

It's as simple as that. All other considerations are secondary.

Why virtual function call is faster than dynamic_cast?

The virtual function call is similar to a function pointer, or if the compiler knows the type, static dispatch. This is constant time.

dynamic_cast is quite different -- it uses an implementation defined means to determine a type. It is not constant time, may traverse the class hierarchy (also consider multiple inheritance) and perform several lookups. An implementation may use string comparisons. Therefore, the complexity is higher in two dimensions. Real time systems often avoid/discourage dynamic_cast for these reasons.

More details are available in this document.

How fast is dynamic_cast

It depends hugely on your compiler, your particular class hierarchy, the hardware, all sorts of factors. You really need to measure it directly inside your particular application. You can use rdtsc or (on Windows) QueryPerformanceCounter to get a relatively high-precision timer for that purpose. Be sure to time loops or sleds of several thousand dynamic_cast<>s, because even QPC only has a ¼μs resolution.

In our app, a dynamic_cast<> costs about 1 microsecond, and a string comparison about 3ns/character.

Both dynamic_cast<> and stricmp() are at the top of our profiles, which means the performance cost of using them is significant. (Frankly in our line of work it's unacceptable to have those functions so high on the profile and I've had to go and rewrite a bunch of someone else's code that uses them.)

Performance hit from C++ style casts?

If the C++ style cast can be conceptualy replaced by a C-style cast there will be no overhead. If it can't, as in the case of dynamic_cast, for which there is no C equivalent, you have to pay the cost one way or another.

As an example, the following code:

int x;
float f = 123.456;

x = (int) f;
x = static_cast<int>(f);

generates identical code for both casts with VC++ - code is:

00401041   fld         dword ptr [ebp-8]
00401044 call __ftol (0040110c)
00401049 mov dword ptr [ebp-4],eax

The only C++ cast that can throw is dynamic_cast when casting to a reference. To avoid this, cast to a pointer, which will return 0 if the cast fails.

Time complexity of typeid and dynamic_cast operations in C++

The language standard doesn't impose any requirements about the complexity of either operation. As such, the statement that you quote isn't backed up by specification. It is presumably based on either how the functionality can be, or has been implemented in practice.

Bigger problem with the claim of preferability is that it is quite unclear how dynamic_cast even could be used in a non-polymorphic context.


If you're doing object oriented dynamic polymorphism, then what you should prefer is virtual functions.



Related Topics



Leave a reply



Submit