-
Notifications
You must be signed in to change notification settings - Fork 1
/
8_joblib.py
43 lines (30 loc) · 1.03 KB
/
8_joblib.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
40
41
42
43
'''
https://joblib.readthedocs.io/en/latest/
joblib allows memoization:
memoization is an optimization technique used primarily to speed up computing by storing the results
of expensive function calls and returning the cached result when the same inputs occur again.
'''
import os
import time
from joblib import Parallel, delayed, Memory # store ops on disk (not RAM!)
cachedir = './cache'
mem = Memory(cachedir, verbose=1)
x=10000000
@mem.cache
class CalculateSquare:
def run(self):
s=0
for n in range(x):
s+=n*n
return s
if __name__ == "__main__":
tasks = [CalculateSquare() for cpu in range(os.cpu_count())]
start = time.time()
task_output = Parallel(n_jobs=os.cpu_count())(delayed(CalculateSquare.run)(tasks) for i in tasks)
end = time.time()
print(f'Work took {end - start} seconds')
print(f'Sum tasks output: {sum(x for x in task_output)} ')
'''
Parallel sets the parallelization over many processes (same as Pool)
delayed converts the function in a future object
'''