Last active
January 2, 2016 19:09
-
-
Save jamwt/8348851 to your computer and use it in GitHub Desktop.
Fib with Python, Kaleidoscope, and C. gcc -O2
ghc --make -O2
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
# Python | |
[hsenv]jamwt@tango:~/contrib/kaleidoscope/src/chapter7$ cat test.py | |
def fib(x): | |
if x < 3: | |
return 1 | |
else: | |
return fib(x-1)+fib(x-2) | |
print fib(45) | |
[hsenv]jamwt@tango:~/contrib/kaleidoscope/src/chapter7$ python test.py | |
1134903170 | |
real 3m20.244s | |
user 3m20.268s | |
sys 0m0.036s | |
# Kaleidoscope | |
[hsenv]jamwt@tango:~/contrib/kaleidoscope/src/chapter7$ cat test.k | |
def fib(x) | |
if x < 3 then | |
1 | |
else | |
fib(x-1)+fib(x-2); | |
# here | |
fib(45); | |
[hsenv]jamwt@tango:~/contrib/kaleidoscope/src/chapter7$ ./Main test.k | |
Evaluated to: 1.13490317e9 in 5.838595s | |
# C | |
[hsenv]jamwt@tango:~/contrib/kaleidoscope/src/chapter7$ cat test.c | |
#include <inttypes.h> | |
#include <stdio.h> | |
uint64_t fib(int x) { | |
return x < 3 ? 1 : fib(x-1) + fib(x-2); | |
} | |
int main(int argc, char **argv) { | |
uint64_t res = fib(45); | |
printf("%llu\n", (long long unsigned)res); | |
return 0; | |
} | |
[hsenv]jamwt@tango:~/contrib/kaleidoscope/src/chapter7$ ./a.out | |
1134903170 | |
real 0m1.794s | |
user 0m1.792s | |
sys 0m0.000s | |
# Bonus round... Haskell | |
[hsenv]jamwt@tango:~/contrib/kaleidoscope/src/chapter7$ cat test.hs | |
fib 1 = 1 | |
fib 2 = 1 | |
fib n = (fib $ n - 1) + (fib $ n - 2) | |
main = print $ fib 45 | |
[hsenv]jamwt@tango:~/contrib/kaleidoscope/src/chapter7$ ghc --make test.hs -O2 | |
[1 of 1] Compiling Main ( test.hs, test.o ) | |
Linking test ... | |
[hsenv]jamwt@tango:~/contrib/kaleidoscope/src/chapter7$ time ./test | |
1134903170 | |
real 0m40.489s | |
user 0m40.424s | |
sys 0m0.072s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment