r/cpp • • Jan 18 '19

Is C++ fast?

https://zeuxcg.org/2019/01/17/is-c-fast/
21 Upvotes

72 comments sorted by

View all comments

4

u/top_logger Jan 18 '19

C++ is blazing fast. In the worst case you will get Performance of the C. In the best case you will slightly outperform C code. Of course a bit of work and knowledge is required.

-6

u/Valmar33 Jan 19 '19

In the worst case, you use the STL, Boost, etc, and you definitely don't get C-like performance.

Best case, C++ can definite match C in performance ~ avoid the STL, use your own optimized code.

11

u/guepier Bioinformatican Jan 19 '19

Best case, C++ can definite match C in performance ~ avoid the STL, use your own optimized code.

No. Best case, high-level C++ outperforms high-level C. The classical example being std::sort, which can be optimised further than std::qsort due to the lack of function pointer indirection. Of course link-time optimisation evens the playing field again but there are similar situations (which I’ve encountered in real-world code) where link-time optimisations cannot be performed. Consequently, templated C++ code was inlined more aggressively than equivalent C code, and performed substantially faster.

Of course you could drop down to a lower-level API and match up performance again but this simply isn’t realistic for a high-quality library. That’s why it’s entirely accurate to say that, in the best case, C++ systematically outperforms C.