Created
May 24, 2014 11:54
-
-
Save anonymous/2eabd5714c1bd4f37a7b to your computer and use it in GitHub Desktop.
Cython && Go
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
/* | |
filename: fib.go | |
go build fib.go | |
time ./fib>/dev/null | |
------------------- | |
real 0m3.718s | |
user 0m3.719s | |
sys 0m0.008s | |
------------------ | |
*/ | |
package main | |
import ( | |
"fmt" | |
) | |
func fibonacci(n int) int64 { | |
if n>1 { | |
return fibonacci(n-1) + fibonacci(n-2) | |
} | |
return 1 | |
} | |
func main(){ | |
for i:=0; i<=40; i++ { | |
fmt.Println(i, fibonacci(i)) | |
} | |
} |
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
""" | |
filename: fib.pyx | |
cython --embed fib.pyx | |
gcc fib.c -lpython2.7 -I/usr/include/python2.7 -o fib | |
time ./fib>/dev/null | |
------------ | |
-O0 | |
real 0m3.847s | |
user 0m3.842s | |
sys 0m0.004s | |
------------ | |
-O1 | |
real 0m2.201s | |
user 0m2.193s | |
sys 0m0.008s | |
------------ | |
-O2 | |
real 0m1.179s | |
user 0m1.179s | |
sys 0m0.000s | |
------------ | |
""" | |
cdef long fibonacci_c(long n): | |
return fibonacci_c(n-1)+fibonacci_c(n-2) if n>1 else 1 | |
fibonacci = lambda long n: fibonacci_c(n) | |
for i from 0<=i<=40: | |
print i,fibonacci(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment