If you write bad code it's gonna run slow, surprise surprise. The c++ version involves creating multiple temporaries and allocating on the heap. This is not what string operator+ is for. If we want to be fair (and actually use the language as intended) rerun the benchmark with std::format or rather fmt::format_to because snprintf doesn't allocate but uses a buffer on stack just like format_to would.
I'll take a wild guess and say you aren't very experienced with this stuff. First of all, once the code is compiled to IR, most backends are the same (rustc uses llvm just like clang does for example). So much of C and C++ code runs through the same optimization pipelines. The advantage for C++ is, for example some of the computation can be made once during compilation (constexpr, consteval, TMP) etc. and sometimes its stronger type system can lead to optimization wins too.
Not to say the language is without faults, it's obviously not. But these sentiments about C being inherently faster are just not true. Just like statements like C++ is inherently faster than Rust aren't true. Well written code is the most important factor in it being fast.
2
u/granadesnhorseshoes Dec 08 '20
except its not really a 2 cm stone and the platform aint a 10 passenger jet. It's a 75lb Buddha statue and the platforms a single engine Cessna.
It'll still go just fine and your happy to have it along but it aint free and its usefulness is entirely in who you ask.