UCL-RITS / rse-classwork-2020

4 stars 113 forks source link

Improving performance using Numba and Cython #195

Open ageorgou opened 3 years ago

ageorgou commented 3 years ago

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.

image

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).

Using Numba

  1. Look at the implementation using numba: calc_pi_numba.py
  2. Discuss how different it looks to the original. Is it more/less readable? Can you understand what the differences mean?
  3. 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.

  1. Open the Jupyter notebook in calc_pi_cython.ipynb.
  2. As before, discuss how different the code looks to the original.
  3. Use %timeit to compare with the runtime of the Numba version and the original code.
  4. From what you have read or know, can the Cython performance be further improved?