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

Measuring performance and using numpy #43

Open
dpshelio opened this issue Dec 3, 2024 · 2 comments
Open

Measuring performance and using numpy #43

dpshelio opened this issue Dec 3, 2024 · 2 comments
Labels
preparation Exercises to do before the class week10 Performance

Comments

@dpshelio
Copy link
Contributor

dpshelio commented Dec 3, 2024

This exercise is the first in a series. The series will look at the execution time of calculating π using the same algorithm, but different implementations and libraries.

This exercise initially uses pure Python to accomplish this approximation of π. The code is already written, and you can find it in the pi_calculation repository.
Your job is to understand the code, measure how much time it takes to complete, and then adapt it to use numpy instead of pure Python.

Step 1: Measuring how long code takes using timeit

The code uses the timeit module from the standard library. There are different ways you can use timeit: either as a module or from its own command-line interface. Check out timeit's documentation to see the different possibilities. Our calc_pi.py wraps the module implementation of timeit, and provides a similar interface to the command line interface of timeit.

Your task:

  1. Run the code with the default values using python calc_pi.py.
  2. Now run it by specifying values for some arguments (to see which arguments you can use, use --help or look at the source code )
  3. In case you would like to time a function (like calculate_pi in this case) without writing all that boilerplate, you can run
    python -m timeit -n 100 -r 5 -s "from calc_pi import calculate_pi_timeit" "calculate_pi_timeit(10_000)()"
    Try it!
  4. Try to understand the source code in more depth:
    • What does calculate_pi_timeit function do, roughly?
    • How does timeit.repeat work?
    • Why do we repeat the calculation multiple times?
    • Can you think of any changes that could make the code faster?

Step 2: Using numpy

The course notes describe how using the numpy library can lead to faster and more concise code.

Your task:

  1. Complete the file calc_pi_np.py so that it does the same as calc_pi.py, but uses numpy arrays instead of lists. Update the functions accordingly (you can change their arguments if it makes more sense for your new version).
    Hint: Instead of creating n x and y values independently, generate an array of size (n, 2).
  2. Which version of the code is faster, the one that uses numpy or the one that uses pure Python?

When you have completed the exercise, react to this issue using the available emojis, or post your comparison of times below!

@dpshelio dpshelio added preparation Exercises to do before the class week10 Performance labels Dec 3, 2024
@rn-phelan
Copy link

% python calc_pi_numpy.py
pi = 3.1476 (with 10000)
100 loops, best of 5: 143 μsec per loop

% python calc_pi.py
pi = 3.1612 (with 10000)
100 loops, best of 5: 1.89 msec per loop

Numpy method is 10x quicker

@r4b61t
Copy link

r4b61t commented Dec 8, 2024

❯ python pi_calculation/calc_pi.py
pi = 3.142 (with 10000)
100 loops, best of 5: 3.25 msec per loop
❯ python pi_calculation/calc_pi_numpy.py
pi = 3.1308 (with 10000)
100 loops, best of 5: 239 μsec per loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preparation Exercises to do before the class week10 Performance
Projects
None yet
Development

No branches or pull requests

3 participants