Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
python vs cython vs ctypes + c, naive benchmark sources


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


$ cython2 *.pyx
$ gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.7 -o timing_cython.c
$ gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.7 -o 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('./').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):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment