Last active
August 27, 2018 20:34
-
-
Save benauthor/b85f5db738e490f7284b8fdfbfc5768d to your computer and use it in GitHub Desktop.
Ballpark reference on function call, thread, process overhead
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Ballpark reference on function call, thread, process overhead | |
bender@dishoftheday:work$ python3 overhead.py 1000 | |
forloop: 0.030994415283203125 ms | |
funcloop: 0.17786026000976562 ms | |
threadloop: 110.0301742553711 ms | |
threadloop2: 85.56795120239258 ms | |
threadloop_p: 77.57711410522461 ms | |
procloop: 5104.6202182769775 ms | |
procloop2: 5000.197172164917 ms | |
procloop_p: 1917.1161651611328 ms | |
bender@dishoftheday:work$ python2 overhead.py 1000 | |
forloop: 0.0360012054443 ms | |
funcloop: 0.0820159912109 ms | |
threadloop: 104.093790054 ms | |
threadloop2: 105.156898499 ms | |
threadloop_p: 104.53414917 ms | |
procloop: 2793.98608208 ms | |
procloop2: 2815.98496437 ms | |
procloop_p: 1101.86100006 ms | |
bender@dishoftheday:work$ | |
""" | |
from functools import wraps | |
from multiprocessing import Process | |
from threading import Thread | |
import sys | |
import time | |
def _timing(func): | |
@wraps(func) | |
def inner(*args, **kwargs): | |
t0 = time.time() | |
result = func(*args, **kwargs) | |
t1 = time.time() | |
print("%s: %s ms" % (func.__name__, (t1 - t0) * 1000)) | |
return result | |
return inner | |
def noop(): | |
pass | |
class NoopThread(Thread): | |
def run(self): | |
pass | |
class NoopProc(Process): | |
def run(self): | |
pass | |
@_timing | |
def forloop(n): | |
for i in range(n): | |
pass | |
@_timing | |
def funcloop(n): | |
for i in range(n): | |
noop() | |
@_timing | |
def threadloop(n): | |
for i in range(n): | |
thread = NoopThread() | |
thread.start() | |
thread.join() | |
@_timing | |
def threadloop2(n): | |
for i in range(n): | |
thread = Thread(target=noop) | |
thread.start() | |
thread.join() | |
@_timing | |
def threadloop_p(n): | |
threads = [] | |
for i in range(n): | |
threads.append(NoopThread()) | |
for t in threads: | |
t.start() | |
for t in threads: | |
t.join() | |
@_timing | |
def procloop(n): | |
for i in range(n): | |
proc = NoopProc() | |
proc.start() | |
proc.join() | |
@_timing | |
def procloop2(n): | |
for i in range(n): | |
proc = Process(target=noop) | |
proc.start() | |
proc.join() | |
@_timing | |
def procloop_p(n): | |
procs = [] | |
for i in range(n): | |
procs.append(NoopProc()) | |
for p in procs: | |
p.start() | |
for p in procs: | |
p.join() | |
if __name__ == "__main__": | |
n = int(sys.argv[1]) | |
for test in ( | |
forloop, | |
funcloop, | |
threadloop, | |
threadloop2, | |
threadloop_p, | |
procloop, | |
procloop2, | |
procloop_p, | |
): | |
test(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment