| ▲ | hackrmn 28 minutes ago | |
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). | ||