Last active
December 15, 2015 14:50
-
-
Save jansegre/487054963e18a25cce35 to your computer and use it in GitHub Desktop.
Small fibonacci bench (Rust, Swift, C)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fib bench (Rust, Swift, C) I added C to the discussion on | |
https://www.reddit.com/r/rust/comments/3wxi9x/why_is_rust_much_slower_than_swift_in_this_simple/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <inttypes.h> | |
#include <stdio.h> | |
int64_t fib(int64_t n) { | |
if (n <= 2) { | |
return 1; | |
} else { | |
return fib(n - 1) + fib(n - 2); | |
} | |
} | |
int main() { | |
int64_t num = 45; | |
printf("%"PRIi64"\n", fib(num)); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fib.rs // I've tried also i32, usize, but it is the same. I'm on 64bit | |
fn fib(n: i64) -> i64 { | |
if n <= 2 { | |
1 | |
} else { | |
fib(n - 1) + fib(n - 2) | |
} | |
} | |
fn main() { | |
let num = 45; | |
println!("{}", fib(num)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fib(n: Int) -> Int { | |
if n <= 2 { | |
return 1 | |
} else { | |
return (fib(n - 1) + fib(n - 2)) | |
} | |
} | |
let num = 45 | |
print(fib(num)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RUSTC := rustc | |
SWIFTC := swiftc | |
.PHONY: all | |
all: fib_rust fib_swift fib_c | |
fib_rust: fib.rs | |
$(RUSTC) -O $< -o $@ | |
fib_swift: fib.swift | |
$(SWIFTC) -O $< -o $@ | |
fib_c: fib.c | |
$(CC) -O $< -o $@ | |
.PHONY: bench | |
bench: fib_rust fib_swift fib_c | |
@echo | |
@$(RUSTC) --version | |
@time ./fib_rust | |
@echo | |
@echo | |
@$(SWIFTC) --version | |
@time ./fib_swift | |
@echo | |
@echo | |
@$(CC) --version | |
@time ./fib_c | |
@echo | |
@echo | |
.PHONY: clean | |
clean: | |
rm -f fib_rust fib_swift fib_c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment