David Tanzer recently blogged about comparing performance of equivalent C and C++ programs. He did so in order to be able to decide which of the two languages he should use as implementation language for his diploma thesis. In C, he used a classical dispatching mechanism, while in C++, he used inheritance and virtual methods. His result was that C++ was about 7 times slower with his test program. I couldn't believe this, and so I conducted my own tests, with a slightly modified source which can be downloaded
here. I conducted tests of binaries with different optimization levels, all done with gcc/g++ 4.2.3 as provided by Debian unstable:
Optimization | C (average of 3 runs; [s]) | C++ (average of 3 runs; [s]) |
---|
-Os | 38.778 | 34.001 |
-O3 | 27.951 | 32.722 |
-O2 -fomit-frame-pointer | 27.985 | 31.562 |
-O0 | 33.271 | 34.192 |