Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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