Created
December 29, 2018 18:30
-
-
Save mykhailokrainik/daf934db061635509e4bd25fab75274e to your computer and use it in GitHub Desktop.
Fibonacci
This file contains hidden or 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
# Crystal cfib.cr | |
def fib(n) | |
if n <= 2 | |
1 | |
else | |
fib(n - 1) + fib(n - 2) | |
end | |
end | |
puts fib(42) | |
$ crystal build cfib.cr --release | |
$ strip cfib | |
# cfib size is 306480 byte | |
# Rust rfib.cr | |
pub fn fib(n: i32) -> u64 { | |
if n <= 2 { | |
1 | |
} else { | |
fib(n - 1) + fib(n - 2) | |
} | |
} | |
fn main() { | |
println!("{}", fib(42)); | |
} | |
$ rustc -C opt-level=3 rfib.rs | |
$ strip rfib | |
# rfib size is 194920 byte | |
$ time ./cfib | |
267914296 | |
real 0m0.696s | |
user 0m0.691s | |
sys 0m0.004s | |
$ time ./rfib | |
267914296 | |
real 0m0.646s | |
user 0m0.642s | |
sys 0m0.004s | |
# <-- This is 42 line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment