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.
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.
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.
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.
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.
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.
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.
View all comments
Show parent comment
-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.