-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
35 lines (29 loc) · 872 Bytes
/
test.py
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
29
30
31
32
33
34
35
from random import random
from multiprocessing import Pool
import timeit
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__':
N = 10**7 # total iterations
P = 1 # number of processes
p = Pool(P)
print(timeit.timeit(lambda: print(f'{sum(p.map(find_pi, [N//P]*P))/P:0.7f}'), number=10))
p.close()
p.join()
print(f'{N} total iterations with {P} processes')
P = 5 # number of processes
p = Pool(P)
print(timeit.timeit(lambda: print(f'{sum(p.map(find_pi, [N//P]*P))/P:0.7f}'), number=10))
p.close()
p.join()
print(f'{N} total iterations with {P} processes\n')