大家猜猜,下面几种,哪个快?
using Distributed
using SharedArrays
n = 1000000
println("n =>:",n)
println("async iter:")
@time arr1 = iter_async(n)
println("sharedarray iter:")
@time arr2 = iter_sharedarray(n)
println("async sharedarray iter:")
@time arr3 = iter_async_sharedarray(n)
function iter_async(n)
arr = Array{Float64}(undef,n)
@async for i in 1:n
arr[i] = sin(i^2)
end
return arr
end
function iter_sharedarray(n)
arr = Array{Float64}(undef,n)
arr = SharedArray(arr)
for i in 1:n
arr[i] = sin(i^2)
end
return arr
end
function iter_async_sharedarray(n)
arr = Array{Float64}(undef,n)
arr = SharedArray(arr)
@async for i in 1:n
arr[i] = sin(i^2)
end
return arr
end
没想到吧,好东西并不是简单叠加在一起,1+1>2的。
julia> @run testfun
n =>:1000000
async iter:
0.005512 seconds (9.04 k allocations: 8.132 MiB)
sharedarray iter:
0.042330 seconds (19.18 k allocations: 8.653 MiB)
async sharedarray iter:
0.017621 seconds (11.02 k allocations: 8.238 MiB, 23.24% gc time)
iter_async_sharedarray (generic function with 1 method)