Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sontek
Created October 23, 2012 03:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sontek/3936459 to your computer and use it in GitHub Desktop.
Save sontek/3936459 to your computer and use it in GitHub Desktop.
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