5 ms·
Came here to say almost exactly this. C++ is fast. All the things mentioned in the article are very specific replacements of C++ standard library implementation
by mynegation 8y ago
Came here to say almost exactly this. C++ is fast. All the things mentioned in the article are very specific replacements of C++ standard library implementations. C++ is a zero cost abstraction language, all these things could have been replaced with idiomatic C++. The fact that they were implemented in C is more of an authors language preference. Reimplementing some things might be easier in C because C is a simpler (and often more familiar) language. Idiomatic C++ may require knowledge of pretty arcane feature of already complicated language, like placement new and judicious use of references and move operators.
- stephencanon 8y agoAll the things that were replaced were abstractions, which had costs. I think that there's a valid point that a lot of C++ programmers use these abstractions liberally, while sweeping the very real costs under the rug. There are also benefits to using them, of course.
- lasagnaphil 8y agoI would say C++ is more like a zero runtime-cost abstraction language (in release builds). You could achieve good release build runtime-speeds in modern idiomatic C++, but you have to trade off your compile times and Debug-build runtime-speeds for that.
- vardump 8y ago> I would say C++ is more like a zero runtime-cost abstraction language (in release builds). Well, other than the "zero-cost" features that increase code size. For example careless use of heavy template classes can generate a lot of code. In other words, in C++ it's very easy to generate a lot of code from seemingly innocent constructs. Instruction cache misses, page faults (and to lesser amount TLB misses) are not zero cost.
- ben509 8y agoTo your point, cost (or strength) should always consider size as well as time. It would be nice to have a tool that could compute the size impact of a given line.
- FreezerburnV 8y agoI said this elsewhere, but I would legitimately like to see a program like what the author wrote, written in idiomatic C++ that performs as well as what the author got to. Maybe in a comparison style like the author to show the slower C++, the faster "C with classes", and the comparable speed "idiomatic C++" or something. I feel like I see people saying that idiomatic C++ is just as fast as something, while other people say that idiomatic C++ is slower and should be replaced. I'm going to assume you can for sure write C++ using the STL to be just as fast, and would like to learn what needs to be done to get to that point so I can incorporate those techniques into my own C++ code where I might need that extra bit of performance. (I'm playing around with writing games in C++, so performance is more of a concern than an IO-heavy web server. Or I'm considering learning QT for making apps, etc.)
- mynegation 8y agoAuthor posted C version of their code[1]. As a couple of examples of how you can go back to idiomatic C++ starting from here: 1. replace all pointers by passing references to templatized vectors. On a high level of optimization they will be compiled to the same pointer arithmetic as C, but will give you type-safety on the source level. 2. Replace structs with clasess with short inlined accessors. On a high level of optimization they will be compiled to the same memory offset access 3. _If_ there is any value in making domain-specific sort generic - make it such [1] https://gist.github.com/zeux/bf847986e0474cf48f61bb5749da38e4 https://gist.github.com/zeux/bf847986e0474cf48f61bb5749da38e...