C++ 14 Apr 2010 20:41:21

C++ dynamic_cast Performance

A performance comparison of the speed of dynamic_cast operations in C++.

Idea from http://www.nerdblog.com/2006/12/how-slow-is-dynamiccast.html, who did not provide any source so I wrote my own more extensive tests.

Sources

Things Tested

  • reinterpret_cast on a known type
  • virtual function call + reinterpret_cast
  • member variable access + reinterpret_cast
  • successful dynamic_cast to its own type
  • successful dynamic_cast from the derived levels to lower levels
  • failed dynamic_cast from the derived levels to an unrelated type

Observations

  • 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

Linux: GNU g++ 4.4.1

  • Compiler: GNU g++ 4.4.1 -std=c++0x -O3
  • Arch: Linux 2.6.27 x86_64, 2.66GHz Xeon, 8 GiB RAM
g++ 4.4.1TicksRelative Factor
reinterpret_cast known-type60081601.00
virtual function + reinterpret_cast110172481.83
member variable + reinterpret_cast80132161.33
dynamic_cast same-type-base success70093921.17
dynamic_cast same-type-level1 success309310085.15
dynamic_cast same-type-level2 success304426885.07
dynamic_cast same-type-level3 success304784325.07
dynamic_cast level1-to-base success60081761.00
dynamic_cast level2-to-base success70095041.17
dynamic_cast level3-to-base success60081761.00
dynamic_cast level2-to-level1 success360557766.00
dynamic_cast level3-to-level1 success431783207.19
dynamic_cast level3-to-level2 success360562566.00
dynamic_cast onebase-to-twobase fail270423364.50
dynamic_cast onelevel1-to-twobase fail330512325.50
dynamic_cast onelevel2-to-twobase fail390961286.51
dynamic_cast onelevel3-to-twobase fail9944582416.55

Linux: LLVM clang++

clang++ 1.5 (trunk 101368)TicksRelative Factor
reinterpret_cast known-type80120001.00
virtual function + reinterpret_cast140194241.75
member variable + reinterpret_cast80118721.00
dynamic_cast same-type-base success70082240.87
dynamic_cast same-type-level1 success590863527.37
dynamic_cast same-type-level2 success660963208.25
dynamic_cast same-type-level3 success12217899215.25
dynamic_cast level1-to-base success80120161.00
dynamic_cast level2-to-base success80119681.00
dynamic_cast level3-to-base success80116961.00
dynamic_cast level2-to-level1 success670976808.37
dynamic_cast level3-to-level1 success731056169.12
dynamic_cast level3-to-level2 success741094569.25
dynamic_cast onebase-to-twobase fail290399043.62
dynamic_cast onelevel1-to-twobase fail370560644.63
dynamic_cast onelevel2-to-twobase fail440636165.50
dynamic_cast onelevel3-to-twobase fail10634465613.27

Windows: MSVC++ 2010 Express

  • Compiler: MSVC++ 2010 Express _SECURE_SCL=0
  • Arch: Windows 7 64 bit, 1.83GHz Core2Duo, 4 GiB RAM
VC++ 2010TicksRelative Factor
reinterpret_cast known-type60045261.00
virtual function + reinterpret_cast110084701.83
member variable + reinterpret_cast60045701.00
dynamic_cast same-type-base success60045481.00
dynamic_cast same-type-level1 success7074310111.78
dynamic_cast same-type-level2 success10173652516.94
dynamic_cast same-type-level3 success13529929622.53
dynamic_cast level1-to-base success70057901.17
dynamic_cast level2-to-base success60042731.00
dynamic_cast level3-to-base success60042291.00
dynamic_cast level2-to-level1 success11712382019.51
dynamic_cast level3-to-level1 success15748894726.23
dynamic_cast level3-to-level2 success14907660924.83
dynamic_cast onebase-to-twobase fail8498430614.15
dynamic_cast onelevel1-to-twobase fail10499412017.49
dynamic_cast onelevel2-to-twobase fail13041835421.72
dynamic_cast onelevel3-to-twobase fail15114255225.17
  • StumbleUpon
  • Facebook
  • Digg
  • Google Buzz
  • Delicious
  • Reddit
  • MySpace
  • Share/Bookmark

2 Responses to “C++ dynamic_cast Performance”

  1. on 16 Apr 2010 at 14:53:31 1.Jorn said …

    It would be nice to see windows tested on a comparable machine

  2. on 16 Apr 2010 at 18:12:56 2.Tino Didriksen said …

    I agree, but that would be a lot of extra work…

    Right now, I can develop and run the tests simultaneously on my own Windows computer and via SSH on the Linux machine. Having to reboot to Linux just to re-run the tests would be painful. There is also the Cygwin or MinGW options, but while they are nice they really cannot be called a representative picture of how g++ performs.

    Plus, the tests I do are not about compiler vs. compiler; they’re about general methods and which ones reliably perform well cross-platform.

Subscribe to the comments through RSS Feed

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word