美文网首页
julia 矢量化 宏【@.】 的简单测试

julia 矢量化 宏【@.】 的简单测试

作者: 昵称违法 | 来源:发表于2020-03-13 13:15 被阅读0次
using Statistics
function fn2()
    sum = 0
    f(x) = x+1
    map(f,1:10000000)
end

function fn20()
    sum = 0
    f(x) =@. x+1
    f(1:10000000)
end

function fn21()
    sum = 0
    f(x) = x+1
    1:10000000 .|> f
end

function fn22()
    sum = 0
    f(x) = @. x+1
    1:10000000 |> f
end

function fn23()
    summ = 0
    res = Array{Int64,1}(undef,10000000)
    for i in 1:10000000
        res[i] = i + 1        
    end    
end

测试第一次

@time fn2()
@time fn20()
@time fn21()
@time fn22()
@time fn23()

测试结果:

0.086563 seconds (143.22 k allocations: 83.826 MiB, 19.46% gc time)
0.012236 seconds (59.20 k allocations: 3.153 MiB)
0.141388 seconds (244.70 k allocations: 88.733 MiB, 48.50% gc time)
0.012723 seconds (64.92 k allocations: 3.407 MiB)
0.091222 seconds (24.10 k allocations: 77.649 MiB, 57.02% gc time)

测试第二次

@time fn2()
@time fn20()
@time fn21()
@time fn22()
@time fn23()

测试结果:
0.044647 seconds (6 allocations: 76.294 MiB, 31.30% gc time)
0.000001 seconds (5 allocations: 192 bytes)
0.040441 seconds (6 allocations: 76.294 MiB, 22.06% gc time)
0.000001 seconds (5 allocations: 192 bytes)
0.036968 seconds (6 allocations: 76.294 MiB, 22.04% gc time)

相关文章

网友评论

      本文标题:julia 矢量化 宏【@.】 的简单测试

      本文链接:https://www.haomeiwen.com/subject/vcmcshtx.html