You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Run the code with the default values using python calc_pi.py.
Now run it by specifying values for some arguments (to see which arguments you can use, use --help or look at the source code )
In case you would like to time a function (like calculate_pi in this case) without writing all that boilerplate, you can run
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).
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!
The text was updated successfully, but these errors were encountered:
❯ 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
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 usetimeit
: either as a module or from its own command-line interface. Check outtimeit
's documentation to see the different possibilities. Ourcalc_pi.py
wraps the module implementation oftimeit
, and provides a similar interface to the command line interface oftimeit
.Your task:
python calc_pi.py
.--help
or look at the source code )calculate_pi
in this case) without writing all that boilerplate, you can runcalculate_pi_timeit
function do, roughly?timeit.repeat
work?Step 2: Using
numpy
The course notes describe how using the
numpy
library can lead to faster and more concise code.Your task:
calc_pi_np.py
so that it does the same ascalc_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
andy
values independently, generate an array of size(n, 2)
.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!
The text was updated successfully, but these errors were encountered: