forked from UCL-RITS/rse-classwork-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Measuring performance and using numpy UCL-RITS#185
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import numpy as np | ||
import argparse | ||
import random | ||
from utils import format_time | ||
|
||
def point_in_circle(coord, radius=1): | ||
""" | ||
Checks whether a point (x, y) is part of a circle with a set radius. | ||
example | ||
------- | ||
>>> point_in_circle(0, 0) | ||
True | ||
""" | ||
r = np.sqrt(np.square(coord).sum(axis=1)) | ||
return r[r<radius] | ||
|
||
def calculate_pi_timeit(points): | ||
""" | ||
Wrapper function to build calculate_pi with a particular number of points | ||
and returns the function to be timed. | ||
""" | ||
def calculate_pi(): | ||
""" | ||
Calculates an approximated value of pi by the Monte Carlo method. | ||
""" | ||
coord = np.random.rand(points,2) | ||
within_circle = point_in_circle(coord) | ||
return 4 * sum(within_circle)/points | ||
|
||
return calculate_pi | ||
|
||
|
||
def command(): | ||
""" | ||
entry point of the script to accept arguments | ||
""" | ||
|
||
parser = argparse.ArgumentParser(description="Calculates an approximate value of PI and how long it takes", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
parser.add_argument('--npoints', '-np', default=10_000, type=int, help="Number of random points to use") | ||
parser.add_argument('--number', '-n', default=100, type=int, help="Number of times to execute the calculations") | ||
parser.add_argument('--repeat', '-r', default=5, type=int, help="How many times to repeat the timer") | ||
|
||
arguments = parser.parse_args() | ||
|
||
calc_pi = calculate_pi_timeit(arguments.npoints) | ||
print(f"pi = {calc_pi()} (with {arguments.npoints})") | ||
result = timeit.repeat(calc_pi, number=arguments.number, repeat=arguments.repeat) | ||
best = min(result) / arguments.number | ||
print(f"{arguments.number} loops, best of {arguments.repeat}: {format_time(best)} per loop") | ||
|
||
|
||
if __name__ == '__main__': | ||
command() |