Skip to content

Instantly share code, notes, and snippets.

@nknytk
Created August 9, 2020 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nknytk/16643fda5ed18087ad083c2b5cd0674d to your computer and use it in GitHub Desktop.
Save nknytk/16643fda5ed18087ad083c2b5cd0674d to your computer and use it in GitHub Desktop.
Python3.8並列処理 データ受け渡し方法によるデータ送受信速度の計測
import ctypes
import queue
import threading
import multiprocessing
from multiprocessing.managers import SharedMemoryManager
from time import time
def main():
test_configs = [
# test name, test func, args
('Thread + Queue', test_queue, (threading.Thread, queue.Queue)),
('Thread + mp.Queue', test_queue, (threading.Thread, multiprocessing.Queue)),
('Process + mp.Queue', test_queue, (multiprocessing.Process, multiprocessing.Queue)),
('Process + mp.Value', test_mp_value, None),
('Process + SharedMemory', test_shared_mem, None),
]
header_row = ['data size'] + [c[0] for c in test_configs]
print(','.join(header_row))
for data_name, data in generate_data(64 * 1024**2, 100, 1024**3):
results = [data_name]
for test_name, test_func, args in test_configs:
if args:
results.append(str(test_func(data, *args)))
else:
results.append(str(test_func(data)))
print(','.join(results))
del(data)
del(data_name)
def generate_data(max_size=1024**3, n_data=1000, max_total_bytes=1024**3):
data_bytes = 1
while True:
if data_bytes < 1024:
data_name = '{}B'.format(data_bytes)
elif data_bytes < 1024**2:
data_name = '{}KiB'.format(int(data_bytes/1024))
elif data_bytes < 1024**3:
data_name = '{}MiB'.format(int(data_bytes/1024**2))
num_data = min(int(max_total_bytes / data_bytes), n_data)
yield data_name, [b'1' * data_bytes for i in range(num_data)]
if num_data < 20 or data_bytes > max_size:
break
data_bytes *= 2
def test_queue(data, worker_class, queue_class):
""" queueを使ってworkerにデータを渡す速度を計測する """
q = queue_class()
worker = worker_class(target=read_queue, args=(q, ))
worker.start()
s = time()
for d in data:
q.put(d)
q.put(None)
worker.join()
return (time() - s) / len(data) * 1000
def read_queue(q):
while True:
if (d := q.get()) is None:
break
else:
v = len(d)
def test_multi_queue(data, worker_class, queue_class):
""" queueを使ってworkerにデータを渡す速度を計測する """
qs = [queue_class(), queue_class()]
worker = worker_class(target=read_multi_queue, args=(qs,))
worker.start()
s = time()
for i, d in enumerate(data):
qs[i % 2].put(d)
for q in qs:
q.put(None)
worker.join()
return time() - s
def read_multi_queue(qs):
valid_q_idx = list(range(len(qs)))
while valid_q_idx:
for i, q_idx in enumerate(valid_q_idx):
if (d := qs[q_idx].get()) is None:
del(valid_q_idx[i])
break
else:
v = len(d)
def test_shared_mem(data):
sm = multiprocessing.shared_memory.SharedMemory(create=True, size=len(data[0]))
input_q = multiprocessing.Queue()
output_q = multiprocessing.Queue()
worker = multiprocessing.Process(target=read_shared_mem, args=(sm, input_q, output_q))
worker.start()
s = time()
for d in data:
sm.buf[:] = d
input_q.put(1)
output_q.get()
input_q.put(None)
worker.join()
e = time()
sm.close()
sm.unlink()
return (e - s) / len(data) * 1000
def read_shared_mem(sm, input_q, output_q):
while input_q.get() is not None:
v = len(sm.buf)
output_q.put(1)
def test_mp_value(data):
shared_val = multiprocessing.Value(ctypes.c_char_p, data[0])
input_q = multiprocessing.Queue()
output_q = multiprocessing.Queue()
worker = multiprocessing.Process(target=read_shared_val, args=(shared_val, input_q, output_q))
worker.start()
s = time()
for d in data:
shared_val.value = d
input_q.put(1)
output_q.get()
input_q.put(None)
worker.join()
return (time() - s) / len(data) * 1000
def read_shared_val(shared_val, input_q, output_q):
while input_q.get() is not None:
v = len(shared_val.value)
output_q.put(1)
if __name__ == '__main__':
main()
data size Thread + Queue Thread + mp.Queue Process + mp.Queue Process + mp.Value Process + SharedMemory
1B 0.005002021789550781 0.023703575134277344 0.032176971435546875 0.1212763786315918 0.12701034545898438
2B 0.0037717819213867188 0.020499229431152344 0.027980804443359375 0.14542579650878906 0.09492635726928711
4B 0.0037336349487304688 0.01608133316040039 0.03010272979736328 0.10924816131591797 0.09382247924804688
8B 0.003745555877685547 0.016417503356933594 0.02943277359008789 0.11422395706176758 0.08771657943725586
16B 0.003983974456787109 0.016350746154785156 0.025451183319091797 0.10820388793945312 0.09582281112670898
32B 0.0037169456481933594 0.016303062438964844 0.030164718627929688 0.10610580444335938 0.09850502014160156
64B 0.0036716461181640625 0.02480030059814453 0.03183603286743164 0.09764909744262695 0.10260343551635742
128B 0.0036644935607910156 0.01652240753173828 0.026655197143554688 0.08958578109741211 0.10970830917358398
256B 0.0037527084350585938 0.017309188842773438 0.024869441986083984 0.0905299186706543 0.10367393493652344
512B 0.004019737243652344 0.04436969757080078 0.028417110443115234 0.10870218276977539 0.10608434677124023
1KiB 0.005819797515869141 0.022983551025390625 0.027289390563964844 0.09598731994628906 0.09950876235961914
2KiB 0.003807544708251953 0.023682117462158203 0.03299713134765625 0.10034322738647461 0.11030435562133789
4KiB 0.0038123130798339844 0.025069713592529297 0.038073062896728516 0.10784387588500977 0.10396242141723633
8KiB 0.0038003921508789062 0.028924942016601562 0.041060447692871094 0.1165008544921875 0.10722160339355469
16KiB 0.003941059112548828 0.038046836853027344 0.034177303314208984 0.13592243194580078 0.1042795181274414
32KiB 0.004456043243408203 0.039043426513671875 0.04447221755981445 0.1455402374267578 0.10692119598388672
64KiB 0.006351470947265625 0.0634622573852539 0.06305932998657227 0.18308401107788086 0.11087417602539062
128KiB 0.004811286926269531 0.10075569152832031 0.12464761734008788 0.2627992630004883 0.13004541397094727
256KiB 0.005745887756347656 0.21264076232910156 0.23287057876586914 0.37282228469848633 0.1333475112915039
512KiB 0.004677772521972656 0.3526449203491211 0.3123140335083008 0.7114386558532715 0.16408920288085938
1MiB 0.004906654357910156 0.8446598052978516 0.8431696891784668 1.278979778289795 0.2594113349914551
2MiB 0.005269050598144531 1.81654691696167 1.9718575477600098 2.217888832092285 0.40178537368774414
4MiB 0.0045299530029296875 3.061826229095459 3.1145238876342773 3.867309093475342 0.7125759124755859
8MiB 0.005066394805908203 6.037204265594482 6.215560436248779 7.282800674438477 1.3335061073303223
16MiB 0.005815178155899048 12.423943728208542 12.72214949131012 15.073873102664948 2.423703670501709
32MiB 0.00899285078048706 65.27037918567657 63.637495040893555 42.54037141799927 4.960961639881134
64MiB 0.014171004295349121 156.10168874263763 132.39239156246185 87.10524439811707 10.924965143203735
data size Thread + Queue Thread + mp.Queue Process + mp.Queue Process + mp.Value Process + SharedMemory
1B 0.038831233978271484 0.12784242630004883 0.1531815528869629 0.4248619079589844 0.4993557929992675
2B 0.04471778869628906 0.13806581497192383 0.17821073532104492 0.49850940704345703 0.5087137222290039
4B 0.04281044006347656 0.12654542922973633 0.13678550720214844 0.4379153251647949 0.4173851013183594
8B 0.04157304763793945 0.12609481811523438 0.12987375259399414 0.4292607307434082 0.41295289993286133
16B 0.0438237190246582 0.12635231018066406 0.16495227813720703 0.4223775863647461 0.4130125045776367
32B 0.03949880599975586 0.13243913650512695 0.12618541717529297 0.4203343391418457 0.5350208282470703
64B 0.04072904586791992 0.12611865997314453 0.15360355377197266 0.421903133392334 0.4224205017089844
128B 0.03900289535522461 0.12946844100952148 0.1263880729675293 0.4392075538635254 0.41703224182128906
256B 0.041441917419433594 0.1305866241455078 0.13291358947753906 0.8023357391357422 0.5164909362792969
512B 0.03931760787963867 0.14002084732055664 0.13332128524780273 0.4275989532470703 0.41575193405151367
1KiB 0.041985511779785156 0.14795780181884766 0.14319181442260742 0.4292154312133789 0.42220115661621094
2KiB 0.04277944564819336 0.1592087745666504 0.1501011848449707 0.4384636878967285 0.4386448860168457
4KiB 0.04194021224975586 0.17183542251586914 0.15837430953979492 0.45262575149536133 0.4361081123352051
8KiB 0.03947734832763672 0.1811361312866211 0.16770601272583008 0.4542708396911621 0.7958221435546875
16KiB 0.07993459701538086 0.39174795150756836 0.3568863868713379 0.5501031875610352 0.4456138610839844
32KiB 0.04133939743041992 0.2715325355529785 0.24551153182983398 0.516655445098877 0.4579353332519531
64KiB 0.0394749641418457 0.5136919021606445 0.5005216598510742 0.5951929092407227 0.49261569976806635
128KiB 0.0393366813659668 1.0733270645141602 1.295182704925537 0.7829570770263672 0.5845856666564941
256KiB 0.03981351852416992 1.9383645057678223 2.018139362335205 1.0773968696594238 1.0971903800964355
512KiB 0.0391697883605957 5.68084716796875 5.353026390075684 2.4362921714782715 1.085522174835205
1MiB 0.040361881256103516 12.326762676239014 10.176982879638672 3.9967060089111333 1.6806316375732422
2MiB 0.04050731658935547 22.875592708587646 19.61162805557251 7.288973331451416 2.598898410797119
4MiB 0.04122033715248108 44.991448521614075 38.79650682210922 13.486742973327637 4.523750394582748
8MiB 0.04271417856216431 88.43235671520233 77.68793404102325 26.057034730911255 8.754253387451172
16MiB 0.05313754081726074 175.67460238933563 153.5910964012146 52.168115973472595 17.273619771003723
data size Thread + Queue Thread + mp.Queue Process + mp.Queue Process + mp.Value Process + SharedMemory
1B 0.008401870727539062 0.037343502044677734 0.04545927047729492 0.18274307250976562 0.1517009735107422
2B 0.008203983306884766 0.04199028015136719 0.039293766021728516 0.1788330078125 0.18006563186645508
4B 0.011477470397949219 0.04183053970336914 0.04070758819580078 0.18567562103271484 0.17441749572753906
8B 0.011246204376220703 0.04114866256713867 0.042672157287597656 0.18831729888916016 0.15976905822753906
16B 0.011453628540039062 0.04155874252319336 0.04181623458862305 0.18205642700195312 0.18001079559326172
32B 0.011398792266845703 0.04126548767089844 0.04059791564941406 0.17915725708007812 0.17944574356079102
64B 0.011475086212158203 0.042877197265625 0.04601001739501953 0.16271591186523438 0.13425827026367188
128B 0.00888824462890625 0.041565895080566406 0.042421817779541016 0.17552852630615234 0.1637434959411621
256B 0.011386871337890625 0.042891502380371094 0.04329681396484375 0.17569303512573242 0.18358945846557617
512B 0.011594295501708984 0.04379987716674805 0.04143714904785156 0.16567468643188477 0.15178680419921875
1KiB 0.008692741394042969 0.04824638366699219 0.04398822784423828 0.1664137840270996 0.17976760864257812
2KiB 0.01115560531616211 0.05356311798095703 0.04693031311035156 0.18112897872924805 0.16547441482543945
4KiB 0.01142263412475586 0.0562286376953125 0.04801511764526367 0.16214609146118164 0.1535654067993164
8KiB 0.011408329010009766 0.06258726119995117 0.050411224365234375 0.14738082885742188 0.19160032272338867
16KiB 0.011856555938720703 0.06931066513061523 0.054683685302734375 0.17348051071166992 0.1940608024597168
32KiB 0.011491775512695312 0.07317304611206055 0.06365537643432617 0.2121114730834961 0.19851446151733398
64KiB 0.011494159698486328 0.10931015014648438 0.08779048919677734 0.253756046295166 0.19918203353881836
128KiB 0.011599063873291016 0.1478290557861328 0.14328479766845703 0.31339168548583984 0.2013874053955078
256KiB 0.008759498596191406 0.23842811584472656 0.2741050720214844 0.41857481002807617 0.18804550170898438
512KiB 0.009074211120605469 0.4596996307373047 0.38632631301879883 0.6116080284118652 0.20103693008422852
1MiB 0.008504390716552734 0.7840824127197266 0.6919050216674805 1.346588134765625 0.30914306640625
2MiB 0.008482933044433594 1.6222596168518066 1.4360523223876953 2.3607850074768066 0.3931999206542969
4MiB 0.00820159912109375 3.808901309967041 3.108069896697998 4.180455207824707 0.5405998229980469
8MiB 0.00810384750366211 6.060199737548828 5.364480018615723 6.725549697875977 1.1523652076721191
16MiB 0.008575618267059326 12.08101212978363 10.546207427978516 12.981578707695007 2.072412520647049
32MiB 0.006139278411865234 53.61878126859665 45.8793044090271 32.35800564289093 3.8864240050315857
64MiB 0.011742115020751953 112.51465976238251 92.60928630828857 63.97116184234619 8.021771907806396
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment