cyheap, a python extension of minimum heap, is originally implemented by C and wrapped to a python extension by Cython.
This project is intended to be a tutorial regarding HOWTO wrap a C library by Cython to be a efficient python extension.
cyheap consists of three parts, the minimum heap dynamic library(implemented in C), its python extension(wrapped by Cython), and the related benchmark applications. In addition to the official tutorial, it serves as a moderate example to show how to wrap a C library by Cython, e.g. how to define structures, functions, python extension and take advantage of C libraries.
Through introducing compile-time type information and wrapping the C library by Cython, under the same benchmark environment, cyheap is 40+ times faster than its counterpart, which is implemented in pure Python with same algorithm. Further, it is almost four times faster than the heapq - the standard library of heap in Python.
Owing to its high performance, numpy has been taking full advantage of Cython to speed up the efficience of modules for scientific computing and other time critical scenarios.
-
Build and install the C library
$ make $ sudo make install
-
Setup the python extension
$ python setup.py build_ext --inplace
-
Test the python extension
$ python -m unittest test_minheap or by the nosetests. $ nosetests
Note that sudo make install
will copy the library libminheap.so.1.0.0 to /usr/lib, then call the ldconfig
automatically, finally make a symbol link libminheap.so to libminheap.so.1. So if you want to uninstall the library completely, you need to remove all these files manually.
$ make clean
-
To run benchmarks.
-
Benchmark of python extension(minheap)
$ python benchmark_minheap.py
-
Benchmark of pure python implementation of minheap
$ python benchmark_pyheap.py
-
Benchmark of heap from the python standard library-heapq
$ python benchmark_heapq.py
-
-
To use the pyheap.
$ python >>>from minheap import MinHeap >>>heap = MinHeap() >>>heap.push({"id":1, "price":1.0}) >>>item = heap.peek() >>>item = heap.pop()
Note that peek or pop from a empty heap will raise an IndexError.
-
To run the test.
-
Test the C library
$ make test_array $ make test_heap $ ./test_array $ ./test_heap
-
Test minheap
$ python test_minheap.py
-
- C library files
- array.h/c, minheap.h/c, test_array.c, test_heap.c
- Cython files
- cyminheap.pxd, cyminheap.pyx, cyminheap.c, setup.py
- Benchmark and test files
- benchmark_heapq/minheap/pyheap.py, test_minheap.py
enjoy!