| ▲ | Writing Efficient C++ Code(asawicki.info) |
| 52 points by ibobev 2 days ago | 12 comments |
| |
|
| ▲ | hn_submit an hour ago | parent | next [-] |
| I write in C++ almost every day but never have the need to optimize for speed. Even when you write straightforward code it's already blazingly fast. |
| |
| ▲ | gbin 26 minutes ago | parent | next [-] | | It is probably very domain specific. In robotics for example everything is a zero sum game: CPU, memory bandwidth, GPU, battery life etc ... So it is really a topic, probably true for anything embedded actually. Some other offline applications: HFT, Telco etc..
I wish the GUI apps devs respect more the laptop resources they are running on, don't get me started on the 4 instances of chrome I need to run just for discord, signal etc ... | |
| ▲ | cjbgkagh 28 minutes ago | parent | prev [-] | | I rarely use C++ but when I do it is for speed. It’s not uncommon that carefully crafted intrinsics can 10x the straightforward naive implementation. |
|
|
| ▲ | 112233 2 hours ago | parent | prev | next [-] |
| "This article was originally published in Polish in issue 4/2013" — a lot of excellent advice. Sad to see C++ have moved in last decade in a direction that makes writing efficient, simple low level code harder and harder :( |
| |
| ▲ | fooblaster an hour ago | parent | next [-] | | How? you can write exactly the same low level code today. | | |
| ▲ | beached_whale an hour ago | parent | next [-] | | My thought too. There are so many things that are expressible in C++ now that could not be without writing much more code or using per-compilation tools back then. The ability to run code at compile time that is not run at runtime is huge, #embed lets us make other tools output available without linker scripts or compiler specific tools that. Also, most of the code from the past still works(from 10 years ago definitely works) | |
| ▲ | AlotOfReading 42 minutes ago | parent | prev [-] | | Shot in the dark, but maybe the OP is referring to the fact that these code conventions are explicitly discouraged by the C++ core guidelines. The SoA example falls afoul of the rule requiring T* to be used only for singular object pointers, for example. | | |
| ▲ | cjbgkagh 30 minutes ago | parent [-] | | Not a regular C++ programmer but wouldn’t you use std::span here instead? Sure it’ll carry a few redundant lengths but it makes using functions that take spans easier. When I do write C++ it’s usually for speed so I’m often working at the intrinsics level, though AI has gotten good enough at it that I now generally delegate this work to an agent. |
|
| |
| ▲ | jll29 an hour ago | parent | prev [-] | | I think it has become EASIER: for instance, since C++23 Rust-like move semantics can be used, which provides the compiler with extra information that can be leveraged for the generation of better code. Or take constexpr - it permits to move computations to compile time that are complex and in older versions either had to be done at runtime, or an ugly workaround had to be used (e.g. assigning a mysterious literal pre-computed in another run or by hand). | | |
| ▲ | creata 25 minutes ago | parent [-] | | > C++23 Rust-like move semantics can be used What C++23 feature allows that? |
|
|
|
| ▲ | MaxBarraclough 29 minutes ago | parent | prev [-] |
| There's no mention of branch prediction, or context switching, or synchronisation. Depending on what you're doing, they could be very consequential. There's only very brief mention of parallelisation with threads and with SIMD. High-performance programming is a big topic. The scope is far too broad for a single blog post, which naturally gives only cursory discussion of C++ and computer architecture. The article isn't bad considering, but I do think it's the wrong format. A blog series, or even a book, would be more fitting. |
| |
| ▲ | glouwbug 4 minutes ago | parent [-] | | Learn which instructions SIMD nicely (sqrt / fabs, etc). Use ternaries in loops for masking. Use trig identities and lookup tables (don't recompute sin(3t) when you can use two vector multiples using a table of sin(t) eg. sin(t) * sin(t) * sin(t)). Use divisible constexpr constants in loops to eliminate the SIMD tail. Be careful with type casts and floats. `float x; x += 0.5` will introduce *cvt instructions even if the compiler statically knew better otherwise (use 0.5f). Compile with --fast-math and friends so errno doesn't invalidate your SIMD pipeline. |
|