In [1]:
import sys
sys.path.append('..')

In [2]:
import utils
import imp
imp.reload(utils)


Out[2]:
<module 'utils' from '../utils.py'>

In [3]:
import numpy as np
from joblib import Parallel, delayed
from itertools import product
from time import sleep

In [8]:
def power_it(number, n=2):
    # We add a sleep in here to simulate heavy work that 
    # needs the parallelizing:
    sleep(0.1)
    return number ** n

In [9]:
my_array = np.arange(100).reshape(10, 10)

In [10]:
in_list = list(my_array.ravel())

In [12]:
results = utils.parfor(power_it, in_list)

In [15]:
results_array = np.array(results).reshape(my_array.shape)

In [18]:
i, j = np.random.randint(0, 9, 2)
assert results_array[i, j] == power_it(my_array[i, j])

In [21]:
%timeit power_it(my_array[i, j])


10 loops, best of 3: 102 ms per loop

In [22]:
%timeit utils.parfor(power_it, in_list, n=3, n_jobs=1)


1 loop, best of 3: 10.2 s per loop

In [23]:
%timeit utils.parfor(power_it, in_list, n=3, n_jobs=2)


1 loop, best of 3: 5.22 s per loop

In [24]:
%timeit utils.parfor(power_it, in_list, n=3, n_jobs=8)


1 loop, best of 3: 1.45 s per loop

In [25]:
import multiprocessing

In [26]:
multiprocessing.cpu_count()


Out[26]:
4

In [ ]: