HACKER Q&A
📣 itsmeadarsh

No BS, which is actually faster C++ or Rust?


No BS, which is actually faster C++ or Rust?


  👤 rzzzwilson Accepted Answer ✓
Testing _your codebase_ written in Rust and C++ is really the only way to answer your question. There are many factors beside raw speed that should be considered when choosing a language. If speed were the only consideration we would all be writing code in assembler.

👤 topsteplocal
The only thing any test proves is, which compiler produces more efficient assembly code.

Specifically, it's LLVM vs all the other C++ compilers.

C++ compilers like GCC generate more optimized asm for some things. Other things LLVM does better than GCC, but so does C++ compiled with LLVM.

C++ is a fantastic language, but it's become far too complicated with people using like 10% of the features, but everyone uses a slightly different 10%, and no one can really know more than 20%... cpp2 is trying to address this, but in the meantime you have new entrants looking to take over the real-time space (rust, zig, etc.)

The honest no BS answer is: it depends... more specifically, you need to do your own benchmarks specifically on the bottlenecks you expect your project to face.


👤 fwsgonzo
Writing performant code is a highly complex and arduous task that is highly dependent on compiler, language, system, hardware and actual needs.

It's also about understand the black box that is your program by using tools beside the programming language to understand memory usage, allocation patterns, blocking operations and dead-time, bloat, destructive interference in multiprocessing applications, NUMA-stuff and so on, and so forth. Then there's program-specific things like SIMD-processing, using efficient algorithms, differences created between standard-library implementations.

So, which language is faster is just a chapter in a book about performance. And, comparing two languages it feels like comparing apples to oranges. If you had the chance to go back and write a program over again, you will probably end up with an overall different, hopefully better, design.


👤 thomashabets2
In theory Rust should be faster because lack of aliasing. But that boils down to "a sufficiently smart compiler", which is a fallacy.

The only time I compared like for like, Rust was faster: https://blog.habets.se/2024/04/Rust-is-faster-than-C.html

But that's a data point, not a proof.


👤 viraptor
Depends on the compiler and version and target and code and levels of optimisation and way the runtime profile is generated and ...

Unless you actually care about ~20% difference, you can assume they're the same. And we can assume you don't care that much, because you'd run the test on your own workload instead of asking. (You wanted a no BS answer)