Optimizing Manipulations Using Cython
See the optimization of string manipulations using Cython.
We'll cover the following...
The previous example uses apply
, and it’s clear by now that we don’t prefer that method because it’s slow. Let’s divert from strings for a minute and look at making the apply
operation quicker using Cython.
Cython is a superset of Python that can compile to native code. To enable it in Jupyter, you’ll need to run the following cell magic:
Press + to interact
%load_ext Cython
Then you can define functions with Cython. We’re going to “cythonize” the between
function as a first step:
Press + to interact
%%cythonimport randomdef between_cy(row):return random.randint(*row.values)
When we benchmark this, it’s no faster than our current code. If we add types to Cython code, we can get a speed increase. ...