| ▲ | hn_submit 2 hours ago | |||||||
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. | ||||||||
| ▲ | hackrmn 4 minutes ago | parent | next [-] | |||||||
I started writing a 3-D rendering library in C++, after having written the equivalent in C. The reason I decided to write it in C++ after C, is not only because I wanted to tap into meta-programming which is facilitated much better with C++, or that I wanted niceties like procedure overloading, but because some things with C++ (or C) aren't automagically optimised -- like if you want to leverage struct-of-array (SoA) memory layouts because it lends to fewer SIMD (AVX in my case) instructions in the rendering pipeline, you do _not_ get that "for free" just writing a single procedure in C++, much less with C. Bot languages are layout-sensitive, I mean this is in part what gives one the speed -- optimising with memory layout for cache locality etc. But you have to do it yourself. Meaning that if you need array-of-struct (AoS) or in fact don't know which path the CPU would prefer, there's no other way than roll up your sleeves and one way or another implement both. The kicker is, in my case I chose C++ because templates allow me to reuse most of the code in the rendering pipeline _regardless_ of whether I go for AoS or SoA layout. I leverage operator overloading to do vector by matrix multplication which is implemented in both variants. I do have to specify the desired variant during building, but I've profiled and for Intel x86 and AVX in my case SoA is an order of magnitude improvement, so I just use that. TL;DR; C++ gives you plenty fast by default, but it's not always enough. The difference between 15 and 45 frames per second is the difference between raw and baked (if it was bread). | ||||||||
| ▲ | flowerbreeze 5 minutes ago | parent | prev | next [-] | |||||||
When writing code for end-user applications, I think it's mostly true. When it's writing code for a database engine, a game engine, a 3d renderer, or anything else that involves heavy data processing, optimization is the core "thing" often and it might not even be a good enough solution without it. Although, a lot of time even then C++ is good enough even then when picking reasonable data structures to represent the data. | ||||||||
| ▲ | gbin an hour ago | parent | prev | 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 ... | ||||||||
| ▲ | mathisfun123 2 minutes ago | parent | prev | next [-] | |||||||
Then you don't work on a product that has any scale <shrug>. | ||||||||
| ▲ | glouwbug 33 minutes ago | parent | prev | next [-] | |||||||
True, but moving from a list of unique polymorphic pointers to a std::variant gains you at least a 2-3x speed up in terms of TLB and cacheline locality. From there, swapping to SOA will net you another 4-8x, so you're looking at nearly 25x improvement by going data first. That may not matter in the unique case of say, games, where rendering a million entities will dwarf the cost of SIMD processing a million entities, but in something like numerical simulations (fluids) or quant it will be warmly welcomed | ||||||||
| ||||||||
| ▲ | cjbgkagh an hour 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. | ||||||||