Skip to content

Instantly share code, notes, and snippets.

@markrwilliams
Last active January 4, 2016 15:29
Show Gist options
  • Select an option

  • Save markrwilliams/8641549 to your computer and use it in GitHub Desktop.

Select an option

Save markrwilliams/8641549 to your computer and use it in GitHub Desktop.
somecython
cimport cython
from libc.stdlib cimport calloc, free
cdef extern from "qsortest.h":
void qsort_char(unsigned char *base, size_t nmemb)
def mvqsort(unsigned char[:] vofi):
qsort_char(&vofi[0], vofi.shape[0])
def cqsort(charlist):
cdef int charnum = len(charlist)
cdef unsigned char *chararray = <unsigned char *>calloc(charnum, sizeof(unsigned char))
if chararray is NULL:
raise MemoryError
cdef i
for i in range(charnum):
chararray[i] = charlist[i]
qsort_char(chararray, charnum)
for i in range(charnum):
charlist[i] = chararray[i]
(cython)mrw@hammal:~/src/python/qsortest$ PYTHONPATH=build/lib.linux-x86_64-2.7/ python -m timeit -n 100 -s 'import test' 'test.test_cqsort()'
100 loops, best of 3: 48.2 msec per loop
(cython)mrw@hammal:~/src/python/qsortest$ PYTHONPATH=build/lib.linux-x86_64-2.7/ python -m timeit -n 100 -s 'import test' 'test.test_mvqsort()'
100 loops, best of 3: 22.8 msec per loop
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules=cythonize("qsortest.pyx")
)
import random
import qsortest
unsorted_chars = bytearray(chr(i) for i in xrange(254)) * 1000
random.shuffle(unsorted_chars)
sorted_chars = bytearray(chr(i)
for i in
xrange(254)
for _ in xrange(1000))
def test_cqsort():
qsortest.cqsort(unsorted_chars)
assert sorted_chars == unsorted_chars
def test_mvqsort():
qsortest.mvqsort(unsorted_chars)
assert sorted_chars == unsorted_chars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment