Last active
January 4, 2016 15:29
-
-
Save markrwilliams/8641549 to your computer and use it in GitHub Desktop.
somecython
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from distutils.core import setup | |
| from Cython.Build import cythonize | |
| setup( | |
| ext_modules=cythonize("qsortest.pyx") | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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