-
Notifications
You must be signed in to change notification settings - Fork 1
/
multi.py
39 lines (28 loc) · 971 Bytes
/
multi.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
36
37
38
39
from pylab import *
import multiprocess as mp
import time as t
def function(N):
total = 0
for i in arange(N):
for j in arange(N):
total += i / ((j + 1) ** 2)
return total
if __name__ == '__main__':
K = 100
t0 = t.time()
# Code for Serial (classical approach)
totals = zeros(K)
for i in arange(K):
totals[i] = function(i)
tElapsed = t.time() - t0
print('Time for serial version is {}'.format(tElapsed))
t0 = t.time()
# Code for Pool
nProcs = mp.cpu_count() # Number of processors (cores).
print('Now Pool')
p = mp.Pool(processes=nProcs)
results = p.map(function, [n for n in range(K)]) # Maps the argument to the function named.
p.close() # Says we are done making processes to send.
p.join() # Blocks until we finish the entirety of the multiprocessing (on all procs).
tElapsed = t.time() - t0
print('Time for pool version is {}'.format(tElapsed))