Which program works faster? Does Rust compiler do things extra fast? Here are our simplest programs on Rust, C, C++, Bash, Python3. They were run 100.000 times and timings are measured.
Listings
Listing HelloWorld on Rust, main.rs
fn main() {
println!("Hello, World!");
}
Listing HelloWorld on C, main.c
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Listing HelloWorld on C++, main.cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
Listing HelloWorld on Bourne Shell, 1.sh
#/bin/sh
echo 'Hello, World!'
Listing HelloWorld on Python3, 1.py
#!/usr/bin/python3
print("Hello, World!")
Compilation
Those Rust, C, C++ programs were compiled with optimization flags to a.out{rs,c,cpp}:
rustc main.rs -C opt-level=3 -o a.out.rs
gcc main.c -O3 -o a.out.c
g++ main.cpp -O3 -o a.out.cpp
Timings on 100.000 runs
C program was the best, and finished in 50 seconds.
time for i in `seq 100000`; do ./a.out.c; done
real 0m49.985s
user 0m34.893s
sys 0m17.967s
C++ program finished in 1 minute 27 seconds.
time for i in `seq 100000`; do ./a.out.cpp; done
real 1m27.213s
user 1m7.641s
sys 0m22.359s
Rust program finished in 1 minute 4 seconds.
time for i in `seq 10000`; do ./a.out.rs; done
real 1m4.209s
user 0m46.681s
sys 0m20.219s
Bourne Shell (sh) script finished in 2 minutes.
time for i in `seq 100000`; do ./1.sh; done
real 1m59.985s
user 1m33.079s
sys 0m29.630s
Python3 finished in 13 minutes 52 seconds.
time for i in `seq 100000`; do ./1.py; done
real 13m51.726s
user 10m55.810s
sys 2m58.401s
Conclusions
My system is Linux, Intel Core i5-9400 CPU @ 2.90GHz.
uname -a
Linux devuan 5.10.0-15-amd64 #1 SMP Debian 5.10.120-1 (2022-06-09) x86_64 GNU/Linux
Sure, HelloWorld is the simplest program for compare performance. When need performance, should write own procedures for call operating system libraries like print text. Rust may include lot of unnecessary stuff, C++ also includes very heavy libraries. Results:
# | Language | Time |
1. | C | 50 sec |
2. | Rust | 64 sec |
3. | C++ | 87 sec |
4. | Bourne Shell | 120 sec |
5. | Python3 | 832 sec |
I search beautiful address on Tron Network, Solana, Terra, that takes much time. I could not find even 3-letters word in Terra address, because they gives only Python program.
With these results, it is clear that Python3 works so slowly, better code with own hands on Rust or C.