You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/cython.rst
+23-7Lines changed: 23 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,8 +14,10 @@ Extending Python with Cython
14
14
.. objectives::
15
15
16
16
- 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.
19
21
20
22
.. callout::
21
23
@@ -198,8 +200,18 @@ We can Cythonize cell contents using the magic `%%cython`:
198
200
199
201
The compiled function can then be called from other cells.
200
202
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
203
215
204
216
205
217
Adding static type information
@@ -395,14 +407,18 @@ Cython (see also `Cython docs <https://cython.readthedocs.io/en/latest/src/quick
395
407
Alternatives to Cython
396
408
----------------------
397
409
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
400
416
to use it from Python, consider using one of the following:
401
417
402
418
- `ctypes <https://docs.python.org/3/library/ctypes.html>`__: part of Python standard library.
403
419
- `CFFI <https://cffi.readthedocs.io/en/stable/index.html>`__: somewhat similar to `ctypes` but with more features and probably better for large projects.
404
420
- `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.
0 commit comments