In [157]:
import numpy as np
import numba
import multiprocessing as mp
from concurrent.futures import ThreadPoolExecutor
from joblib import Parallel, delayed
import matplotlib.pyplot as plt

numpy.vectorize


In [ ]:

Threading and multi-core processing


In [148]:
def plot_one(data, name):
    xs, ys = data.T
    plt.scatter(xs, ys, s=1, edgecolor=None)
    plt.savefig('%s.png' % name)

In [149]:
data = np.random.random((10, 10000, 2))

Single core


In [150]:
%%time

for i, M in enumerate(data):
    plot_one(M, i)


CPU times: user 12.8 s, sys: 103 ms, total: 12.9 s
Wall time: 12.8 s

Threads


In [152]:
%%time
args = [(x, i) for i, x in enumerate(data)]

with ThreadPoolExecutor() as pool:
    pool.map(lambda x: plot_one(*x), args)


CPU times: user 8.83 s, sys: 1.27 s, total: 10.1 s
Wall time: 9.4 s

Processes


In [155]:
%%time
args = [(x, i) for i, x in enumerate(data)]

with mp.Pool() as pool:
    pool.starmap(plot_one, args)


CPU times: user 29.4 ms, sys: 93 ms, total: 122 ms
Wall time: 3.49 s

Parallel comprehensions with joblib


In [159]:
%%time

Parallel(n_jobs=-1)(delayed(plot_one)(x, i) for i, x in enumerate(data))
pass


CPU times: user 139 ms, sys: 114 ms, total: 253 ms
Wall time: 3.72 s

In [ ]: