Skip to content

Commit

Permalink
Measuring performance and using numpy UCL-RITS#185
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttamas committed Dec 24, 2020
1 parent 26b5b8c commit 4b5434c
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions week10/calc_pi_np.py
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()

0 comments on commit 4b5434c

Please sign in to comment.