-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstt.py
More file actions
51 lines (38 loc) · 1.39 KB
/
stt.py
File metadata and controls
51 lines (38 loc) · 1.39 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
"""
Time test code from Sheridan Green's Python Multiprocessing jupyter notebook
https://github.com/galastrostats/general/blob/master/PythonMultiprocessing.ipynb
Minimally modified by Sheila Kannappan June 2017 to allow the user to enter
%timeit stt.main(nproc)
on the ipython command line, where nproc is between 1 and N-1 for an N-core machine
"""
import multiprocessing
import time
def runSimulation(params):
"""This is the main processing function. It will contain whatever
code should be run on multiple processors.
"""
param1, param2 = params
# Example computation
processedData = []
for ctr in range(100000):
processedData.append(param1 * ctr - param2 ** 2)
return processedData
def main(nproc):
# Define the parameters to test
param1 = range(100)
param2 = range(2, 202, 2)
params = zip(param1, param2)
pool = multiprocessing.Pool(nproc) # number of processes
# Parallel map
tic = time.clock()
results = pool.map(runSimulation, params)
toc = time.clock()
# Serial map
tic2 = time.clock()
results = map(runSimulation, params)
toc2 = time.clock()
print('Parallel processing time: %r\nSerial processing time: %r'
% (toc - tic, toc2 - tic2))
print('Speedup factor: %r') % ((toc2-tic2)/(toc-tic))
if __name__ == '__main__':
main()