我正在嘗試通過使用 @async @sync 來并行化這個函式
function INSR_opt(f)
function INSR0_opt(seq)
len = length(seq)
res = seq[end]
@inbounds @sync for i in range(len-2,step=-1,stop=0)
@async res = f([seq[i 1], res])
end
return res
end
return INSR0_opt
end
我使用宏的方式對我來說似乎是正確的,但是如果沒有宏,性能會變得更糟:
122.962 μs (1073 allocations: 69.00 KiB)
使用宏:
154.681 μs (1091 allocations: 69.95 KiB)
我什至嘗試過使用@spawn 而不是@async,但性能仍然不會提高。我檢查了運行的執行緒數,Threads.nthreads()它們是 4
uj5u.com熱心網友回復:
您的代碼是順序的,因為您具有遞回依賴性res- 因此嘗試并行化它既不可能,也可能導致不正確的結果。foldr本質上,您的代碼試圖以一種效率較低且非通用的方式重新實作:
julia> INSR_opt(((a, b),) -> a => b)(1:4)
1 => (2 => (3 => 4))
julia> foldr(=>, 1:4)
1 => (2 => (3 => 4))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/493255.html
