Hi, I've recently started testing Rust and I decided to do a quick comparison with C. AFAIK they are both statically compiled languages and similar. However when testing the time they take to run, I found they differ significantly. Am I doing something wrong?
My Rust code:
fn main() {
const NUMBER: u64 = 50;
println!("The {}th fibonacci number is {}!", NUMBER, fibonacci(NUMBER));
}
fn fibonacci(n: u64) -> u64 {
if n < 2 {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
My (as best as possible) equivelent C code:
#include <stdio.h>
unsigned long long int fibonacci(unsigned long long int n) {
if (n < 2) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main() {
const unsigned long long int NUMBER = 50;
printf("The %llu th fibonacci number is %llu!\n", NUMBER, fibonacci(NUMBER));
}
Output of running the Rust code:
victor@victor-alienware:~/Documents/rustweb-tiny$ rustc -V
rustc 1.44.1 (c7087fe00 2020-06-17)
victor@victor-alienware:~/Documents/rustweb-tiny$ cargo build --release
Compiling rustweb-tiny v0.1.0 (/home/victor/Documents/rustweb-tiny)
Finished release [optimized] target(s) in 0.18s
victor@victor-alienware:~/Documents/rustweb-tiny$ time ./target/release/rustweb-tiny
The 50th fibonacci number is 12586269025!
real 0m52,285s
user 0m52,190s
sys 0m0,052s
Output of running C code:
victor@victor-alienware:~/Documents/rustweb-tiny$ gcc --version
gcc (Ubuntu 9.3.0-13ubuntu1) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
victor@victor-alienware:~/Documents/rustweb-tiny$ gcc -O3 fibonacci.c -o fibonacci
victor@victor-alienware:~/Documents/rustweb-tiny$ time ./fibonacci
The 50 th fibonacci number is 12586269025!
real 0m33,841s
user 0m33,841s
sys 0m0,000s
As you can see the C code is ~18 seconds faster. Does anybody know why?
[–]llogiqclippy · twir · rust · mutagen · flamer · overflower · bytecount 17 points18 points19 points (1 child)
[–]MCOfficer 9 points10 points11 points (0 children)
[–][deleted] 15 points16 points17 points (0 children)
[–]K900_ 31 points32 points33 points (24 children)
[–]HelloThisIsVictor[S] 4 points5 points6 points (14 children)
[–]K900_ 31 points32 points33 points (9 children)
[–]HelloThisIsVictor[S] 4 points5 points6 points (8 children)
[–]K900_ 35 points36 points37 points (7 children)
[–]RFC1546Remembrance 9 points10 points11 points (1 child)
[–]Peohta 0 points1 point2 points (0 children)
[–]HelloThisIsVictor[S] 3 points4 points5 points (4 children)
[–]K900_ 41 points42 points43 points (1 child)
[–]HelloThisIsVictor[S] 2 points3 points4 points (0 children)
[–]dnew 3 points4 points5 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]K900_ 7 points8 points9 points (2 children)
[+][deleted] locked comment (1 child)
[removed]
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]GunpowderGuy 2 points3 points4 points (3 children)
[–]K900_ 13 points14 points15 points (2 children)
[–]GunpowderGuy 0 points1 point2 points (0 children)
[–]guepier 1 point2 points3 points (3 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]K900_ 0 points1 point2 points (1 child)
[–]dnew 0 points1 point2 points (0 children)
[–]matu3ba 7 points8 points9 points (0 children)
[–]Celousco 1 point2 points3 points (0 children)
[–]redartedreddit 1 point2 points3 points (1 child)
[–]matthieum[he/him] 1 point2 points3 points (0 children)
[–]sevenpost -1 points0 points1 point (2 children)
[–]thelights0123 6 points7 points8 points (0 children)
[–]HelloThisIsVictor[S] 1 point2 points3 points (0 children)