Anand Sukumaran
Startup Founder, Software Engineer, Abstract thinker
Co-founder & CTO @ Engagespot (Techstars NYC '24)
Benchmarking Results - Rust, C++. Go, Node, PHP, Python, Ruby
Mar 25, 2023Out of curiosity, I did a benchmark of different programming languages - Rust, C++. Go, Node, PHP, Python, Ruby. The program I used for this benchmark is to find the sum of first 1 Million integers, ie 1 to 1000000.
The code
This is the pseudo code I implemented in each of the language below to find the sum of first 1 million numbers.
1. Start
2. Set a variable "sum" to 0
3. Set a variable "counter" to 1
4. Read value of n from stdin
5. While "counter" is less than or equal to n:
6. Add "counter" to "sum"
7. Increment "counter" by 1
8. Display the value of "sum"
9. End
The code was executed in Macbook M1 Air (16GB RAM).
Results
The results are surprising.
- Rust was blazingly fast with execution time in the range of nanoseconds.
- Python was the worst with executime of 90ms.
- C++ was slower than Rust. But with compiler optimizations enabled, it is slightly faster.
- Nodejs had the same execution time as C++ (without g++ optimization).
Language | Execution Time (ms) |
---|---|
Python 3.11.2 | 90.177 |
Ruby 3.1.2p20 | 48.687 |
PHP 8.2.4 | 13.74 |
Python (Pypy - 7.3.11) | 7.975 |
Node v18.14.0 | 3.989 |
C++ (clang-1400.0.29.202) | 3.504 |
Go 1.20.2 | 0.89 |
Rust (rustc 1.68.0) | 0.000334 |
C++ with O optimization flag | 0.00025 |
[Re-test] With compiler optimization for g++
The significant performance of Rust in this case is because it performed the summing operation during compile time itself (read Constant Folding). The same wasn’t enabled during compilation using g++. After enabling g++ -O
compiler optimization, C++ execution time went down to 0.00025ms
But still, is this comparison fair?
One could argue that, comparing compiled languages with interpreted languages is unfair in this case because the sum is already computed at compile time and hence doesn’t actually reflect the true run-time performance of Rust, Go and C++. That is true. It is just as if we’re saving the sum in a constant and prints it to screen.
So, I updated the program to accept a dynamic input from stdin and performed the test again. The result is -
Language | Execution Time (ms) |
---|---|
Python 3.11.2 | 98.177 |
Ruby 3.1.2p20 | 49.47 |
PHP 8.2.4 | 13.74 |
Python (Pypy - 7.3.11) | 7.975 |
Node v18.14.0 | 3.989 |
Go 1.20.2 | 1.614 |
Rust (rustc 1.68.0) | 1.547 |
C++ (clang-1400.0.29.202) | 1.029 |
This benchmark reflects the true runtime performance the three compiled languages. As expected, execution time of interpreted languages remained unchanged.