問題描述
所以我敲了一些測試代碼,看看多處理模塊在 cpu 綁定工作上與線程相比如何擴展.在 linux 上,我得到了預期的性能提升:
So I knocked up some test code to see how the multiprocessing module would scale on cpu bound work compared to threading. On linux I get the performance increase that I'd expect:
linux (dual quad core xeon):
serialrun took 1192.319 ms
parallelrun took 346.727 ms
threadedrun took 2108.172 ms
我的雙核 macbook pro 顯示相同的行為:
My dual core macbook pro shows the same behavior:
osx (dual core macbook pro)
serialrun took 2026.995 ms
parallelrun took 1288.723 ms
threadedrun took 5314.822 ms
然后我在一臺windows機器上試了一下,得到了一些非常不同的結果.
I then went and tried it on a windows machine and got some very different results.
windows (i7 920):
serialrun took 1043.000 ms
parallelrun took 3237.000 ms
threadedrun took 2343.000 ms
為什么,為什么,Windows 上的多處理方法這么慢?
Why oh why, is the multiprocessing approach so much slower on windows?
這是測試代碼:
#!/usr/bin/env python
import multiprocessing
import threading
import time
def print_timing(func):
def wrapper(*arg):
t1 = time.time()
res = func(*arg)
t2 = time.time()
print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0)
return res
return wrapper
def counter():
for i in xrange(1000000):
pass
@print_timing
def serialrun(x):
for i in xrange(x):
counter()
@print_timing
def parallelrun(x):
proclist = []
for i in xrange(x):
p = multiprocessing.Process(target=counter)
proclist.append(p)
p.start()
for i in proclist:
i.join()
@print_timing
def threadedrun(x):
threadlist = []
for i in xrange(x):
t = threading.Thread(target=counter)
threadlist.append(t)
t.start()
for i in threadlist:
i.join()
def main():
serialrun(50)
parallelrun(50)
threadedrun(50)
if __name__ == '__main__':
main()
推薦答案
進程在 UNIX 變體下更加輕量級.Windows 進程很繁重,需要更多時間才能啟動.線程是在 Windows 上進行多處理的推薦方式.
Processes are much more lightweight under UNIX variants. Windows processes are heavy and take much more time to start up. Threads are the recommended way of doing multiprocessing on windows.
這篇關于python multiprocessing vs threading for cpu bound work on windows and linux的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!