Julia : 用@async提升循环性能

@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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值