Skip to content

Instantly share code, notes, and snippets.

@saethlin
Created January 10, 2017 19:47
Show Gist options
  • Save saethlin/787875cee4329c2f0f3a31d643b4910e to your computer and use it in GitHub Desktop.
Save saethlin/787875cee4329c2f0f3a31d643b4910e to your computer and use it in GitHub Desktop.
Rough benchmark to show Python multiprocessing overhead from inter-process communication
import time
import threading
import multiprocessing
from numba import jit
import numpy as np
thing1 = np.empty((4000, 4000))
thing2 = np.empty((4000, 4000))
@jit(nogil=True)
def bench(thing1, thing2):
return np.sum((thing1 - thing2)**2)
bench(thing1, thing2)
print('Threading')
for n_threads in range(1, 9):
threads = [threading.Thread(target=bench, args=(thing1, thing2)) for _ in range(n_threads)]
start = time.time()
for thread in threads:
thread.start()
for thread in threads:
thread.join()
end = time.time()
print(n_threads, end - start)
@jit
def bench_gil(thing1, thing2):
return np.sum((thing1 - thing2)**2)
bench(thing1, thing2)
print('\nMultiprocessing:')
for n_threads in range(1, 9):
threads = [multiprocessing.Process(target=bench_gil, args=(thing1, thing2)) for _ in range(n_threads)]
start = time.time()
for thread in threads:
thread.start()
for thread in threads:
thread.join()
end = time.time()
print(n_threads, end - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment