This exercise builds on #185. It is part of a series that looks at execution time of different ways to calculate π using the same Monte Carlo approach. In this approach, π is approximated by sampling n random points inside a square with side 1, computing the proportion of those points that fall inside the unit circle, and multiplying that by 4/n.
This exercise uses Numba and Cython to accomplish this approximation of π. A Numba version of the code is already written, and you can find it in calc_pi_numba.py on the week10 branch of this repository. Your job is measure how much time it takes to complete in comparison to #185.
Preparation
The two frameworks we will look at allow you to write Python-looking code and compile it into more efficient code which should run faster. Numba is a compiler for Python array and numerical functions. Cython is a way to program C extensions for Python using a syntax similar to Python.
Both frameworks should come with your conda installation. If not, and you get errors when running the instructions below, use conda or pip to install them (see their websites linked above for instructions).
Discuss how different it looks to the original. Is it more/less readable? Can you understand what the differences mean?
Run the code with python calc_pi_numba.py. How does the time compare to the original?
Using Cython
Next, try to use Cython to approximate π. This part will be easier for users of Linux and OS X, as getting Cython to run on Windows is a little more involved.
Approximating π using Numba/Cython
Introduction
This exercise builds on #185. It is part of a series that looks at execution time of different ways to calculate π using the same Monte Carlo approach. In this approach, π is approximated by sampling n random points inside a square with side 1, computing the proportion of those points that fall inside the unit circle, and multiplying that by 4/n.
This exercise uses Numba and Cython to accomplish this approximation of π. A Numba version of the code is already written, and you can find it in
calc_pi_numba.py
on theweek10
branch of this repository. Your job is measure how much time it takes to complete in comparison to #185.Preparation
The two frameworks we will look at allow you to write Python-looking code and compile it into more efficient code which should run faster. Numba is a compiler for Python array and numerical functions. Cython is a way to program C extensions for Python using a syntax similar to Python.
Both frameworks should come with your conda installation. If not, and you get errors when running the instructions below, use
conda
orpip
to install them (see their websites linked above for instructions).Using Numba
calc_pi_numba.py
python calc_pi_numba.py
. How does the time compare to the original?Using Cython
Next, try to use Cython to approximate π. This part will be easier for users of Linux and OS X, as getting Cython to run on Windows is a little more involved.
calc_pi_cython.ipynb
.%timeit
to compare with the runtime of the Numba version and the original code.