Ask HN: Why is Rust faster than C++ for calculating the sum of 1M numbers?
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 ] threadIt’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)
Apocryphal, from memory, many details probably wrong.