Personal template for a generic cython project setup
- Activate environment (
pipenv shell
) - Run
make
or possibly (hehe)import setup
in the code to rebuild the package
The benchmark problem is counting the number of primes up to a certain value. Trivial algorithm chosen:
- First three stop at n
- Last three (r) stop at
sqrt(n)
This allows to compare the sqrt
evaluation (C vs Python).
Parallel computing using openMP
is tested too, releasing the GIL and multithreading.
Just for declaring types and changing from range
to prange
we get:
- Serial: x12
- Parallel: x24
Using libc.math
:
- Serial: x900
- Parallel: x1700
Overall boost: 3000x
Profiling slows down the code, so the compiler is not automatically set in profiler mode.
When needed, rebuild the package using
make profile
An example of line profiling is given in profile.py
.
Some bugs in line_profiler
for cython, not all functions are profiled.
A workaround is to shift the missing functions at the top of the code.
make hardcore
: globally disables array wrapping, bound checks and enables cdivisionmake force
: forces compilation of already compiled extensions. Slows down the compilation if many.pyx
files are found.