@async 用在循环中,可以大幅提升性能,特别是循环量较大时。代码如下:
1、向量
using Distributed
n = 1000000
println("n :",n)
println("iter_no_async :")
@time value1 = iter_no_async(n)
println("iter_async :")
@time value2 = iter_async(n)
@assert value1 ==value2
function iter_no_async(n)
arr = Array{Float64}(undef,n)
for i in 1:n
arr[i] = sin(i^2)
end
return arr
end
function iter_async(n)
arr = Array{Float64}(undef,n)
@async for i in 1:n
arr[i] = sin(i^2)
end
return arr
end
# 100万 =>9倍
julia> @run testfun
n :1000000
iter_no_async :
0.037306 seconds (16.38 k allocations: 8.526 MiB)
iter_async :
0.004689 seconds (9.04 k allocations: 8.132 MiB)
iter_async (generic function with 1 method)
# 10万=>2倍
julia> @run testfun
n :100000
iter_no_async :
0.011192 seconds (16.38 k allocations: 1.659 MiB)
iter_async :
0.004836 seconds (9.04 k allocations: 1.266 MiB)
iter_async (generic function with 1 method)
# 1万 => 略有提升
julia> @run testfun
n :10000
iter_no_async :
0.007968 seconds (16.38 k allocations: 995.775 KiB)
iter_async :
0.005080 seconds (9.04 k allocations: 592.965 KiB)
iter_async (generic function with 1 method)
# 1000 =>不明显
julia> @run testfun
n :1000
iter_no_async :
0.008439 seconds (16.38 k allocations: 925.807 KiB)
iter_async :
0.007028 seconds (9.03 k allocations: 522.699 KiB)
iter_async (generic function with 1 method)
比如,数组拆分,一个数组要拆分成n个平均的等份,这个时侯,用@async 效果也非常好!性能提升数十倍!
2、矩阵
using Distributed
n = 1000; m = 10
println("n :$n m:$m")
println("iter_no_async :")
@time value1 = iter_no_async(n,m)
println("iter_async :")
@time value2 = iter_async(n,m)
@assert value1 ==value2
function iter_no_async(n,m)
arr = Array{Float64}(undef,n,m)
for i in 1:n
for j in 1:m
arr[i,j] = sin(i^2) +sin(j^2)
end
end
return arr
end
function iter_async(n,m)
arr = Array{Float64}(undef,n,m)
@async for i in 1:n
@async for j in 1:m
arr[i,j] = sin(i^2) +sin(j^2)
end
end
return arr
end
julia> @run testfun
n :1000 m:10
iter_no_async :
0.011082 seconds (20.51 k allocations: 1.164 MiB)
iter_async :
0.005053 seconds (9.26 k allocations: 601.795 KiB)
iter_async (generic function with 1 method)
两层的@async ,性能差不多近2倍。
3、拆分
function split_array(rmap,n)
responses = Vector{Array{Float64}}(undef, n)
row,col = size(rmap)
unit = floor(Int64,row/n)
@async for i in 1:n
if i<n
responses[i] = rmap[(i-1)*unit + 1 : i*unit,:]
else
responses[i] = rmap[(i-1)*unit + 1 : row,:]
end
end
return responses
end