這是因爲pmap旨在爲每個核心執行大量計算,而不是很多簡單的計算。如果你使用簡單的功能作爲你的功能,沿着proccesors的信息移動的開銷就會大於好處。相反,測試該代碼(我用的酷睿i7四個核心運行它):
function fast(x::Float64)
return x^2+1.0
end
function slow(x::Float64)
a = 1.0
for i in 1:1000
for j in 1:5000
a+=asinh(i+j)
end
end
return a
end
info("Precompilation")
map(fast,linspace(1,1000,1000))
pmap(fast,linspace(1,1000,1000))
map(slow,linspace(1,1000,10))
pmap(slow,linspace(1,1000,10))
info("Testing slow function")
@time map(slow,linspace(1,1000,10)) #3.69 s
@time pmap(slow,linspace(1,1000,10)) #0.003 s
info("Testing fast function")
@time map(fast,linspace(1,1000,1000)) #52 μs
@time pmap(fast,linspace(1,1000,1000)) #775 s
對於很多非常小的迭代的並行化,你可以在文檔中使用@parallel,尋找它。