Re:Is C++ fast?
by mynegation on Friday January 18, 2019 @09:53AM
(#18939654)
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.
[ Parent
| on HN ]
Re:Is C++ fast?
by FreezerburnV on Friday January 18, 2019 @10:18AM
(#18939838)
I 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.)
Re:Is C++ fast?
by mynegation on Friday January 18, 2019 @12:50PM
(#18941155)
Author 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/bf847986e0474cf48f61bb5749da38e...
Re:Is C++ fast?
by stephencanon on Friday January 18, 2019 @10:08AM
(#18939760)
All 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.
Re:Is C++ fast?
by lasagnaphil on Friday January 18, 2019 @10:13AM
(#18939809)
I 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.
> 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.