Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Approximating π using Numba/Cython #56

Open
dpshelio opened this issue Dec 15, 2022 · 0 comments
Open

Approximating π using Numba/Cython #56

dpshelio opened this issue Dec 15, 2022 · 0 comments
Labels
week10 Performance

Comments

@dpshelio
Copy link
Contributor

dpshelio commented Dec 15, 2022

This exercise builds on #54. It is part of a series that looks at execution time of different ways to calculate π using the same Monte Carlo approach.

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 file in the pi_calculation repository, on the class branch. Your job is measure how much time it takes to complete in comparison to #46.

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 macOS, as getting Cython to run on Windows is a little more involved.

We will use a notebook for this example, as it lets us see more information about how Cython works.

  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 within the notebook 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?
@dpshelio dpshelio added the week10 Performance label Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
week10 Performance
Projects
None yet
Development

No branches or pull requests

1 participant