Take the 2-minute tour ×

Before reading the question:
This question is not about how useful it is to use dynamic_cast. Its just about its performance.

I've recently developed a design where dynamic_cast is used a lot.
When discussing it with co-workers almost everyone says that dynamic_cast shouldn't be used because of its bad performance (these are co-workers which have different backgrounds and in some cases do not know each other. I'm working in a huge company)

I decided to test the performance of this method instead of just believing them.

The following code was used:

ptime firstValue( microsec_clock::local_time() );

ChildObject* castedObject = dynamic_cast<ChildObject*>(parentObject);

ptime secondValue( microsec_clock::local_time() );
time_duration diff = secondValue - firstValue;
std::cout << "Cast1 lasts:\t" << diff.fractional_seconds() << " microsec" << std::endl;

The above code uses methods from boost::date_time on Linux to get usable values.
I've done 3 dynamic_cast in one execution, the code for measuring them is the same.

The results of 1 execution were the following:
Cast1 lasts: 74 microsec
Cast2 lasts: 2 microsec
Cast3 lasts: 1 microsec

The first cast always took 74-111msec, the following casts in the same execution took 1-3msec.

So finally my questions:
Is dynamic_cast really performing bad?
According to the testresults its not. Is my testcode correct?
Why do so much developers think that it is slow if it isn't?

share|improve this question
Am I missing something? I can't see any code for cast2 or cast3. –  Flexo Oct 29 '10 at 10:17
Who can say what's bad? Does your program perform well enough over all? If so, then the performance isn't bad. Is the total time in dynamic casts a big percentage of your execution time? If not, then worry about other things first. More generally, 74 microseconds is terribly slow for some applications - in my last job, I'd have received and parsed an entire update record from the stock exchange, updated the database and told client apps about it in half the time. If you're interested, then compare it to other ways to get the same behaviour. –  Tony D Oct 29 '10 at 10:27
Having lots of dynamic_casts in the code is a sure indicator of the design problems. –  BЈовић Oct 29 '10 at 11:07
It would be helpful to see a complete "minimal working example" of what you ran so we can repeat and modify your tests. –  Flexo Oct 29 '10 at 11:23
My amazing ability to read your mind and understand how you generated the times for Cast2 and Cast3 allow me to deduce that it will rain herring in Iceland tonight. Compilable code is king. PS.Most casting would be implicit (passing a child object to a function that takes a parent (pointer/reference)). PPS. What are you comparing it against? –  Loki Astari Oct 29 '10 at 15:06
show 1 more comment

4 Answers

up vote 23 down vote accepted

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

share|improve this answer
+1, but 10K iterations is likely not enough. Something like 100 million is better. –  sharptooth Oct 29 '10 at 10:38
@sharptooth: Fair point! –  Oli Charlesworth Oct 29 '10 at 10:51
add comment

Performance is meaningless without comparing equivalent functionality. Most people say dynamic_cast is slow without comparing to equivalent behavior. Call them out on this. Put another way:

If 'works' isn't a requirement, I can write code that fails faster than yours.

There are various ways to implement dynamic_cast, and some are faster than others. Stroustrup published a paper about using primes to improve dynamic_cast, for example. Unfortunately it's unusual to control how your compiler implements the cast, but if performance really matters to you, then you do have control over which compiler you use.

However, not using dynamic_cast will always be faster than using it — but if you don't actually need dynamic_cast, then don't use it! If you do need dynamic lookup, then there will be some overhead, and you can then compare various strategies.

share|improve this answer
+1. Yes, btw every living person eventually dies. Which doesn't mean it is a bad idea to be alive. –  sharptooth Oct 29 '10 at 10:39
add comment

Here are a few benchmarks:

According to them, dynamic_cast is 5-30 times slower than reinterpret_cast, and the best alternative performs almost the same as reinterpret_cast.

I'll quote the conclusion from the first article:

  • dynamic_cast is slow for anything but casting to the base type; that particular cast is optimized out
  • the inheritance level has a big impact on dynamic_cast
  • member variable + reinterpret_cast is the fastest reliable way to
    determine type; however, that has a lot higher maintenance overhead
    when coding

Absolute numbers are on the order of 100 ns for a single cast. Values like 74 msec doesn't seem close to reality.

share|improve this answer
The value he was getting was 74 usec (microseconds), not 74 msec (milliseconds). Even so, it still doesn't sound realistic. –  Wallacoloo Mar 18 '12 at 20:14
add comment

Sorry to say this, but your test is virtually useless for determining whether the cast is slow or not. Microsecond resolution is nowhere near good enough. We're talking about an operation that, even in the worst case scenario, shouldn't take more than, say, 100 clock ticks, or less than 50 nanoseconds on a typical PC.

There's no doubt that the dynamic cast will be slower than a static cast or a reinterpret cast, because, on the assembly level, the latter two will amount to an assignment (really fast, order of 1 clock tick), and the dynamic cast requires the code to go and inspect the object to determine its real type.

I can't say off-hand how slow it really is, that would probably vary from compiler to compiler, I'd need to see the assembly code generated for that line of code. But, like I said, 50 nanoseconds per call is the upper limit of what expect to be reasonable.

share|improve this answer
dynamic_cast needs to access RTTI, this will take cycles. –  doron Oct 29 '10 at 10:59
add comment

Your Answer


