If you want to be present you need send an email to [email protected] with the following object:
[WSCYTHON] Nom Prénom
/!\ Put your real firstname and lastname
In the mail share a .zip
or .tar.gz
file with the contents of your repository.
/!\ Please run python3 setup.py clean
before compressing your project
Any work you do needs to be in the email, if you don't do this or didn't advance into the workshop at all you will be marked absent.
Cython is a language that makes writing C extensions for the Python language as easy as Python itself. In this workshop we will be using Cython to benchmark two different algorithms coded in Python and C(ython).
- Python 3
- Check if installed with:
python3 --version
- Check if installed with:
- Cython
- Install with:
pip3 install cython
- Install with:
- DevTools
- Install with:
- For apt (Ubuntu, Debien...):
sudo apt-get install python-dev
#for pyhton2
sudo apt-get install python3-dev
- For yum (CentOS, RHEL...):
sudo yum install python-devel
sudo yum install python3-devel
- For dnf (Fedora):
sudo dnf install python-devel
sudo dnf install python3-devel
- For apt (Ubuntu, Debien...):
- Install with:
You will find two simple alogrithms made with python. Our job today is to make them work in C(ython).
In the demo/
folder you will find a file named demo.py with the following content:
def pyloop():
# ...
def pycount():
# ...
In the demo/cython/
folder you will find a file named demo.pyx with the following content:
def cloop():
# TODO: Fill this function
def ccount():
# TODO: Fill this function
To compile cython code to C/C++ you will use cythonise. It will create a library (.so
, for those unfamiliar with that extension it's similar to a .a
library like the libmy.a
that you should all be familiar with).
To compile the code you will use the following command:
pip3 install -e .
/!\ Make sure to keep any __init__.py
files you see, they are empty but required for the setup.
In the current directory you will find the bench.py file:
import timeit
def main():
# TODO: Fill this function
if __name__ == "__main__":
main()
Using the timeit module call all 4 functions and print the results.
In this exercise we will rewrite the ccount()
function with a more C-like syntax, create a new function underneath named betterccount()
:
def betterccount():
# TODO: Fill this function
Take a look at the documentation here.
/!\ Make sure that you select Cython
syntax and not Pure Python
in the documentation. Those are two different approaches. Pure python is not necessary for our example.
Let's go back to our bench.py file. Add the new betterccount()
function and compare the results.
As a bonus if this subject interests you, you can look at the assembly code behind all this code.
Here is a website that will allow you to do that: godbolt.org,
You can paste your code in the left editor and look at the assembly operation in the right editor, here is an example with the pyloop()
function