forked from Pierian-Data/Complete-Python-3-Bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.py
More file actions
28 lines (22 loc) · 659 Bytes
/
Copy pathtest2.py
File metadata and controls
28 lines (22 loc) · 659 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from random import random
from multiprocessing import Pool
import timeit
import sys
N = int(sys.argv[1]) # these arguments are passed in from the command line
P = int(sys.argv[2])
def find_pi(n):
"""
Function to estimate the value of Pi
"""
inside=0
for i in range(0,n):
x=random()
y=random()
if (x*x+y*y)**(0.5)<=1: # if i falls inside the circle
inside+=1
pi=4*inside/n
return pi
if __name__ == '__main__':
with Pool(P) as p:
print(timeit.timeit(lambda: print(f'{sum(p.map(find_pi, [N//P]*P))/P:0.5f}'), number=10))
print(f'{N} total iterations with {P} processes')