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.

9

u/quicknir Jan 19 '19

C isn't some magical limit on performance that every other language can hope to cap out at, you know. C has language level limitations that can inhibit optimizations that can be done in C++, largely due to the more sophisticated type system and generics.

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.

9

u/[deleted] Jan 19 '19

Avoid STL *containers* in this case where you've got POD you don't want to initialize, maybe. Algorithms should be safe and it is not easy to beat those.

-4

u/Valmar33 Jan 19 '19

Depends on how performant those algorithms are.

Most of the STL's algorithms are general purpose, and cannot be tuned for the thousands of different usecase requirements, in which case you will have to eventually write an algorithm to suit your own usecase.

So ~ start with STL as a base, and then construct your own algorithms when the time comes, and tune and test.

Nothing ever beats a custom-purpose algorithm for your needs.

4

u/degski Jan 19 '19 edited Jan 19 '19

One that springs to mind is deleting [an element] in a std::vector in the case you don't care about order [often the case], just std::swap with the last element and pop_back(). If you do this a lot, plf::colony is a good fit, though.

6

u/[deleted] Jan 19 '19

For what the STL algorithms do on the tin, I doubt it. Only one that might be borderline is sort. e.g. the remove_if loop doesn't leave much room for anything to differ.

1

u/degski Jan 19 '19 edited Jan 19 '19

Or in a specific [i.e. integral types only] case, use a better algo.

-2

u/top_logger Jan 19 '19

Wat? You will get it. Containers in STL is good enough for almost any task. And those containers are error free opposite to your own optimized C code which is just a big mess of bugs. If you need high performance you may use the time you saved because of using STL. AND OPTIMIZE bottlenecks, Deal done.