9 ms·
I interpret the title to mean, "Is standard usage of C++ language features and the STL competitive with C?". The author is basically showing how you improve per
by throwawaymath 8y ago
I interpret the title to mean, "Is standard usage of C++ language features and the STL competitive with C?". The author is basically showing how you improve performance by selectively tearing out more and more features C++ hands you out of the box. This is characteristic of writing C++ in a more C-like style. In that sense I think the author is correct.
However I'd also say this is something already well known by most C and, particularly, C++ developers. Yes, you can get better performance by reimplementing much of what C++ gives you, and that is typical of writing C code instead of C++. But there's a fair bit of productivity trade off in doing so.
- vectorEQ 8y agoif you don't use any of the slow language features it's fast
- Retra 8y agoIf you use special-purpose tools rather than robust ones, you can get better performance. But nobody wants to ship a language without robust general-purpose tools.
- krapp 8y agoEven the slow language features are fast enough for most use cases, though. I think the actual comparison here isn't between 'slow' and 'fast' but 'probably fast enough' and 'as fast as possible.'
- deng 8y agoI'm pretty sure I could make a faster 'printf' but which only supports '%d'. Does that mean 'C is slow'?
- simias 8y agoI'd say that printf, with its own DSL, is actually pretty slow. It's meant for debugging or "slow" I/O, if you need to write a lot of data it should be avoided. A few years ago for debugging purposes I wanted to hexdump a large amount of data. Turns out that the standard hexdump tool was pretty slow at it, writing a simple C utility that would format the dump "by hand" instead of using printf was about an order of magnitude faster. You have fully static code instead of the interpreted format string, you can unroll all the formatting, you don't have to rewrite the characters that don't change in every line (spaces, delimiters etc...). It's lightning fast but of course not very flexible.
- deng 8y ago> I'd say that printf, with its own DSL, is actually pretty slow. Yes, of course it is. That's why I took it as an example. Since I work on RealTime systems I usually have to use plain 'write' instead. But that is because 'printf' is an incredibly complex function, not because 'C is slow'.
- simias 8y agoAt this point I feel like it's almost a philosophical argument but: - printf is a standard function in C's stdlib. - Because of the nature of the function (using a special purpose format strings and varargs instead of some kind of generic system) the function can't really be aggressively optimized and inlined without compiler magic. This limitation is caused by C's own limitations when it comes to generic programming and type introspection, printf needs to be told explicitly through a "side-channel" (the format string) what types to expect because C lacks the infrastructure to do that by itself. It also means that you can't extend the function for custom types and that you can't type-check the parameters without compiler magic. - Even though some compilers do implement special magic around printf to perform more aggressive optimizations (like replacing it with "puts" if there's no formatting going on for instance) it's still generally trivial to beat its performance in non-trivial case with handwritten code. Given all of the above I would say that makes this particular corner of C rather slow indeed. Or at least slower than it might be compared to an hypothetical language that made it easier to optimize string formatting.
- skrebbel 8y agoYou're entirely right, but I feel like you're trying really hard to misunderstand deng's original comment. It's an apt analogy, partly because of what you wrote. This article's title is as ridiculous as an article titled "Is C fast?" with a story about a faster_printf that only supports %d.
- ghettoimp 8y agoIt's been a long time since I've done C++. I wonder if int x; cout << x; ought to be faster than printf("%d", x), by virtue of knowing the types ahead of time?
- throwawaymath 8y agoNo, not at all! At least not in an absolute sense. But what I mean is that writing in C naturally tends towards writing more special-purpose, bespoke functions because you can't lean on the existing, general-purpose functions of C++ (nor language features). I agree with you about the title, but I also think that if you fix the title the author is making a good point. It's not groundbreaking, but the move "down" from C++ to C does run parallel with the move from more general purpose to more special purpose, in a lot of ways. C dominates embedded after all.
- chriswitts 8y agoYou could check out Nanolog [1] for inspiration. [1] https://github.com/PlatformLab/NanoLog https://github.com/PlatformLab/NanoLog
- vidarh 8y agoThis is one of the defining principles of C++, after all: You pay only for what you use. Conversely, if you do opt-in to using more advanced features, it may come at a cost, but it's all optional. It's been one of the things that have most strongly shaped the evolution of the language and the library, for both good and bad, so absolutely, it is very much well known.
- dfox 8y agoFor me this principle is also what often causes performance issues, because for many advanced features this leads to convoluted language semantics and sub-optimal implementations.
- jura_z 8y ago'You pay only for what you use' Sorry, have you read the article? He is not using any C++20, C++17, C++14... features, just adding a compiler key. And he's paying with compilation time. https://twitter.com/zeuxcg/status/1085781851568914432 https://twitter.com/zeuxcg/status/1085781851568914432 You might say that compilation time is nothing.. But it is C++, compilation time has a huge impact on big codebases.
- vidarh 8y agoHe started out with among others, std::vector. That has tradeoffs, in that the standard specifies requirements that he may or may not need. My point stands. The performance trade-offs you're paying for with various C++ features are extremely well understood, and better documented for C++ than most languages. That doesn't mean there aren't surprises here and there, and obviously there are compiler difference, but the point was that the language is designed so that these tradeoffs are known and documented and for the most part comes as a surprise to people mostly when they don't know the language very well. It is not a surprise that you can improve on things like std::vector by rolling your own that does exactly what you want; for starters if you know your requirements chances are you can speed up initialization, because you don't need to abide by standard semantics. For starters, if you know you're going to assign a fixed number of elements, you don't need to track size and capacity, and you can often avoid the need for bounds checking. As for compilation time, that too is very much a matter of paying for what you're using. You can choose to disable debug info, you can choose to not include headers that are bound to result in lots of time spent on parsing templates. That he's not using many advanced features is irrelevant; his starting point very much had made trade-offs in using functionality that comes with known costs.
- carlmr 8y agoThat's why you measure first, and only if necessary go to these extreme measures if you find that this is the place that really costs you performance. Or if performance is basically your only goal (HFT) then you just need to spend this time on a lot of parts of your code. But then in 99% of code it's a waste of time.
- proginthebox 8y agoWith proper use of constexpr, templates and types, you can make C++ at least as fast as C code without giving up on readability.
- stochastic_monk 8y agoAt least as. Often faster because of easier inlining.
- geezerjay 8y ago> The author is basically showing how you improve performance by selectively tearing out more and more features C++ hands you out of the box. In that case the author is showing what has been widely known by C++ developers for decades. In fact, the "don't use the STL because it's slow" is a mantra which, along with the old "replace STL's default allocators with your customized ones", is repeated ad nauseum in gamedev circles.
- Sharlin 8y agoThe C++ standard library is pretty competitive with C given that C has no standard data structure and algorithms library! It's not even an apples and oranges comparison.