Skip to content

Commit e4c6762

Browse files
committed
Updated cython.rst with Numba link, jupyter demo and better Objectives list
1 parent 7c362e9 commit e4c6762

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

content/cython.rst

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ Extending Python with Cython
1414
.. objectives::
1515

1616
- Understand how compiled extension modules can speed up code execution.
17-
- Understand the basics of Cython.
18-
17+
- Build your first compiled extension module with Cython.
18+
- Learn to optimize your Cython code with static type declarations.
19+
- Learn to use Numpy arrays in Cython code and implement common performance
20+
enhancements for Cythonized arrays.
1921

2022
.. callout::
2123

@@ -198,8 +200,18 @@ We can Cythonize cell contents using the magic `%%cython`:
198200
199201
The compiled function can then be called from other cells.
200202

201-
There is also `%%cython --annotate` (or `%%cython -a` for short) which is
202-
useful for analyzing the generated C code.
203+
.. demo::
204+
205+
There is also `%%cython --annotate`, or `%%cython -a` for short, which is
206+
useful for analyzing the generated C code. Try executing the code for
207+
`add()` with this magic command in Jupyter. Upon doing so:
208+
209+
1. Estimate the amount of interactions with the Python runtime, by the intensity of the yellow background colour.
210+
2. You will be able to inspect the underlying C code.
211+
212+
.. solution::
213+
214+
.. image:: img/cython/jupyter-cython-annotate.png
203215

204216

205217
Adding static type information
@@ -395,14 +407,18 @@ Cython (see also `Cython docs <https://cython.readthedocs.io/en/latest/src/quick
395407
Alternatives to Cython
396408
----------------------
397409

398-
There exists a plethora of other tools and libraries for extending Python with
399-
compiled C code. If you already have a working C/C++ codebase and would like
410+
`Numba <https://numba.pydata.org/>`__ is a tool that compiles Python code to
411+
optimized machine code on the fly without needing a manual compilation step.
412+
It works with Numpy but does not support all of Python's features.
413+
414+
For creating compiled extension modules there are a plethora of tools and
415+
libraries. If you already have a working C/C++ codebase and would like
400416
to use it from Python, consider using one of the following:
401417

402418
- `ctypes <https://docs.python.org/3/library/ctypes.html>`__: part of Python standard library.
403419
- `CFFI <https://cffi.readthedocs.io/en/stable/index.html>`__: somewhat similar to `ctypes` but with more features and probably better for large projects.
404420
- `pybind11 <https://pybind11.readthedocs.io/en/stable/index.html>`__: very robust and modern way of creating extension modules. C++ only.
405-
421+
- `PyO3 <https://pyo3.rs/v0.27.1/>`__ for Rust code.
406422

407423
Further reading
408424
---------------

0 commit comments

Comments
 (0)