Skip to content

Commit eed72ea

Browse files
committed
Add joblib example.
1 parent 6fb1da0 commit eed72ea

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

‎README.rst‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,15 @@ documentation.
120120
TODO
121121
====
122122

123-
- writing: packaging
123+
- picking a license
124124
- badge for google slides
125125
- change documentation links to main branch
126-
- joblib example in profile
126+
- joblib for parallelizing, example in profile
127127
- github page
128128
- point out features in scripts: object-oriented, asserts, tqdm, type hints
129129
- matplotlib, pytest, black in dev install
130130
- manifest file to not include file in package
131131
- GitHub actions for releasing to PyPi when changes to version
132-
- cupy / pytorch compatible
132+
- cupy for GPU
133+
- pytorch compatible
133134
- Cython / C++

‎examples/joblib_example.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Example of using `joblib` to parallelize a loop.
3+
4+
pip install joblib
5+
6+
"""
7+
8+
from joblib import Parallel, delayed
9+
import numpy as np
10+
import time
11+
12+
13+
def f(seed, n, proc_time):
14+
np.random.seed(seed)
15+
x = np.random.randn(n)
16+
time.sleep(proc_time) # simulate some long running process
17+
return np.fft.fft(x)
18+
19+
20+
if __name__ == "__main__":
21+
22+
# Create a list of inputs
23+
n = 2048
24+
n_exp = 100
25+
proc_time = 0.2
26+
n_cpu = 6
27+
28+
# Compare processing time for serial and parallel processing
29+
30+
# Serial processing
31+
start = time.time()
32+
outputs_ser = []
33+
for seed in range(n_exp):
34+
outputs_ser.append(f(seed, n, proc_time))
35+
serial_time = time.time() - start
36+
print(f"Serial processing took {serial_time} seconds")
37+
38+
# Parallel processing
39+
start = time.time()
40+
outputs_par = Parallel(n_jobs=n_cpu)(delayed(f)(seed, n, proc_time) for seed in range(n_exp))
41+
parallel_time = time.time() - start
42+
print(f"Parallel processing took {parallel_time} seconds")
43+
44+
# Speed-up
45+
print(f"Speed-up: {serial_time / parallel_time}")

0 commit comments

Comments
 (0)