Ask HN: Why is Rust faster than C++ for calculating the sum of 1M numbers?

10 points by anandnair ↗ HN
I did a benchmark of Rust, C++, Go, Node, Python, Ruby, etc to understand the execution time of a program that simply calculates the sum of 1 million numbers starting with 1. While C++ took around 3.5ms, Rust completed it in under 50ns. What could have been the reason why Rust performed significantly faster?

Full results can be viewed at - https://anands.me/blog/benchmarking-rust-go-cpp-node

13 comments

[ 2.8 ms ] story [ 44.7 ms ] thread
Rust ran the loop at compile time.
Is that a feature specific to Rust? Can you please share any references to their official docs citing this?
It's a feature of optimizing compilers for decades. OP just didn't compile the C++ code with optimizations enabled.
Enabled compiler optimization for g++ and then the execution time went down to 0.00025, which made it slightly faster than Rust :)
C++ was probably compiled without optimizations. In a compiled binary there won't even be any calculations done - see for yourself https://godbolt.org/z/Mhhzhdr7c
I did it, and now c++ is slightly faster than Rust :)
over here we never say more faster, instead we say fasterer or simply fasterrr
But likely not at calculating that sum (which, nitpicking, isn’t “calculating the sum of 1M numbers”, but “calculating the sum of integers 1 through 1,000,000”), but at printing a constant.

It’s possible the C library you used is faster at converting this particular integer to a string (performance will vary between different ones, and not necessarily with one being always faster than the other) and writing it than rust’s one, but also possible that the C compiler did the string conversion at compile-time, too, and compiled a call to puts (if you printed a newline at the end of your string)

I think there was a story about a mainframe salesman who at demonstration time with the customer started the usual demo program. The perspective customer then suggested to leave for lunch and come back when the machine had finished—but then the printer already started to churn out pages, after just a few seconds. Turned out the new compiler was smarter than the old one and had simply erased all the inconsequential computations of the demo code which made it look like magic.

Apocryphal, from memory, many details probably wrong.

Legend says the customer didn’t buy the mainframe because he didn’t believe the results could be accurate when they came out so quickly. So a programmer was asked to put a sleep of 2700 seconds, slightly less time than the local steakhouse can can serve the prospects lunch.
That reminds me of the first time I tried jikes over javac. Javac took about 45 seconds to compile a hello-world. Jikes ran in well under a second. I thought I had done something wrong it was so much faster
[Update] - I ran the code again with proper compiler optimizations and then C++ performs slightly faster than Rust, thus making it the fastest among all.