嗯,這是一種有趣的注意,如果你運行下面的命令:
import numpy
from multiprocessing import Pool
a = numpy.arange(1000000)
pool = Pool(processes = 5)
result = pool.map(numpy.sin, a)
UnpicklingError: NEWOBJ class argument has NULL tp_new
沒想到的是,有啥繼續,以及:
>>> help(numpy.sin)
Help on ufunc object:
sin = class ufunc(__builtin__.object)
| Functions that operate element by element on whole arrays.
|
| To see the documentation for a specific ufunc, use np.info(). For
| example, np.info(np.sin). Because ufuncs are written in C
| (for speed) and linked into Python with NumPy's ufunc facility,
| Python's help() function finds this page whenever help() is called
| on a ufunc.
yep numpy.sin在c中實現,因此您不能直接在多處理中使用它。
,所以我們有另一個函數把它包起來
PERF:
import time
import numpy
from multiprocessing import Pool
def numpy_sin(value):
return numpy.sin(value)
a = numpy.arange(1000000)
pool = Pool(processes = 5)
start = time.time()
result = numpy.sin(a)
end = time.time()
print 'Singled threaded %f' % (end - start)
start = time.time()
result = pool.map(numpy_sin, a)
pool.close()
pool.join()
end = time.time()
print 'Multithreaded %f' % (end - start)
$ python perf.py
Singled threaded 0.032201
Multithreaded 10.550432
哇,沒想到,要麼,我們甚至使用python的功能以及那裏有對於初學者幾個問題如果它只是一個包裝器與一個純c函數,並且也是複製值的開銷,那麼默認情況下多處理器不會共享數據,因爲每個值都需要前後複製。
也指出,如果得到適當的細分我們的數據:
import time
import numpy
from multiprocessing import Pool
def numpy_sin(value):
return numpy.sin(value)
a = [numpy.arange(100000) for _ in xrange(10)]
pool = Pool(processes = 5)
start = time.time()
result = numpy.sin(a)
end = time.time()
print 'Singled threaded %f' % (end - start)
start = time.time()
result = pool.map(numpy_sin, a)
pool.close()
pool.join()
end = time.time()
print 'Multithreaded %f' % (end - start)
$ python perf.py
Singled threaded 0.150192
Multithreaded 0.055083
那麼,我們能從這個取,多是偉大的,但我們應該始終測試,有時比較一下它的速度更快,有時它更慢,取決於如何其使用...
授予您沒有使用numpy.sin
但另一個函數我建議您首先驗證確實多處理會加快計算速度,也許前後複製值的開銷可能會影響到您。
無論哪種方式,我還認爲,使用pool.map
是多線程代碼是最好的,最安全的方法...
我希望這有助於。
如果你打算使用'pool.map()',你應該使用'math.sin',因爲它比'numpy.sin'更快。參考:http://stackoverflow.com/questions/3650194/are-numpys-math-functions-faster-than-pythons。 – EOL 2014-01-06 19:53:29
對於'numpy.sin',[官方numpy/scipy wiki](http://wiki.scipy.org/ParallelProgramming)表示,如果你[用openmp編譯numpy開啓]它應該並行工作(https:// software.intel.com/en-us/articles/numpyscipy-with-intel-mkl)。 – ziyuang 2015-02-18 14:31:29
你也可以使用[Bohrium](http://bohrium.readthedocs.io/):它應該像用'import bohrium as numpy'替換你的第一行一樣簡單...... – j08lue 2016-05-04 09:33:22