Why is C++ still beating Rust at performance in some places?

As I look at many different benchmarks, I see Rust beating C++ at some specific algorithms; but still it's C++ who wins at most of them.

What is Rust doing slower or otherwise C++ doing faster?

For the specific case of benchmarking algorithms, the answer is likely going to be that the C++ code has received more developer hours on optimizing it.


Since you didn't link the benchmark(s) or describe them in any amount of detail, it's impossible to answer. So the best you can hope for is something generic:

  • Compare the code to ensure they're measuring what you care about. (It doesn't necessarily have to be "apples-to-apples." It really depends on what your goal is.)
  • Read the benchmark methodology and determine for yourself whether the model being used is meaningful to you.
  • Run both programs under a profiler to get an idea of where each is spending time.
  • Read the code generated for each program.

There's a lot more to add, but I don't see a compelling reason to continue.


Googling "Rust vs c++ benchmarks" finds me a lot of results that either claim without source that Rust is faster, show a single specific benchmark, or show a battery of benchmarks that are basically a toss up.

This shouldn't be surprising: Rustc uses the same LLVM backend as Clang as it's optimizing backend, where most of the effect of the language itself in performance comes from. There's a few standard library bits that are probably notably different, and things like aliasing rules can cause large effects on specific cases, but overall they're basically the same ideas after all the compile time checks are done, given to the same backend.


There is fun language benchmark here - mostly for entertainment purposes

It's a simple prime sieve problem - but some surprisingly creative solutions. Zig was #1 last time I looked, but Rust is in the top.

Ultimately it's machine code that gets executed - so no reason two different languages can't achieve similar high performance - unless the language really wasn't meant for it - such as with TeX, SQL or Powershell.

Both Rust and C++ have the same low-level capabilities: both can use inline assembly, or call external functions via a C API. At the language level, both have comparable performance characteristics: they both use manual memory management, both can work directly with memory via raw pointers and bytewise transmutes, both have the same access to SIMD intrinsics, and can precisely control memory layout of data. Finally, both can use the same LLVM backend for optimizations and code generation.

This implies that, in the limit of code maximally optimized at the expense of everything else (including readability and development effort), the performance of two languages should be the same. In fact, it's not uncommon to produce exactly the same assembly output (at least for sufficiently small programs).

Thus the question isn't "which is faster", but "which is faster given 'idiomatic' code with 'reasonable' development effort, readability and maintainability". In this form the question becomes fundamentally subjective, since one can always optimize more at the expense of other code properties.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.