Created
October 23, 2012 03:26
-
-
Save sontek/3936459 to your computer and use it in GitHub Desktop.
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
import ctypes | |
import os | |
import sys | |
here = os.path.dirname(__file__) | |
cpath = os.path.abspath(os.path.join(here, "fib.so")) | |
fiblib = ctypes.CDLL(cpath) | |
def fib(n): | |
if n == 0: | |
return 0 | |
elif n == 1: | |
return 1 | |
else: | |
return fib(n-1) + fib(n-2) | |
if __name__ == '__main__': | |
func = None | |
if len(sys.argv) == 2: | |
print 'Using C! :)' | |
func = fiblib.fib | |
else: | |
print 'Using Python! :(' | |
func = fib | |
for i in range(32): | |
print func(i) | |
#include <stdio.h> | |
/* | |
* compile with: | |
gcc -shared -Wl,-soname,fib -o fib.so -fPIC fib.c | |
*/ | |
int fib(int n) | |
{ | |
if (n == 0) { | |
return 0; | |
} | |
else if (n == 1) { | |
return 1; | |
} | |
else { | |
return fib(n-1) + fib(n-2); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment