Skip to content

Instantly share code, notes, and snippets.

Created November 6, 2012 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save anonymous/4025567 to your computer and use it in GitHub Desktop.
Save anonymous/4025567 to your computer and use it in GitHub Desktop.
python vs cython vs ctypes + c, naive benchmark sources

ctypes:

$ gcc -shared -Wl,-soname,timing_c.so -o timing_c.so -fPIC timing_c.c

cython:

$ cython2 *.pyx
$ gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.7 -o timing_cython.so timing_cython.c
$ gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.7 -o timing_cython_range.so timing_cython_range.c
unsigned long sumrange(unsigned long arg)
{
unsigned long i, x;
x = 0;
for (i = 0; i < arg; i++) {
x = x + i;
}
return x;
}
import ctypes
test = ctypes.CDLL('./timing_c.so').sumrange
test.restype = ctypes.c_ulonglong
test.argtypes = ctypes.c_ulonglong,
cpdef long test(long val):
cdef long sum = 0
while val > 0:
val -= 1
sum += val
return sum
cpdef long test(long val):
cdef long sum = 0
cdef long i = 0
for i in range(val):
sum += i
return sum
def test(val):
sum(xrange(val))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment