gpt4 book ai didi

julia - 别名元组类型时的额外内存分配

转载 作者:行者123 更新时间:2023-12-03 23:01:39 30 4
gpt4 key购买 nike

使用 Julia 1.5.3,我有以下代码(当然它不是实际代码,只是一个人为的示例,我可以用它来重现问题):

function basic()
result::Tuple{Int64,Int64,Int64} = (0, 0, 0)
for i in 1:100000
for i in -1:1, j in -1:1, k in -1:1
result = result .+ (i, j, k)
end
end
result
end

function fancy(T)
result::T = (0, 0, 0)
for i in 1:100000
for i in -1:1, j in -1:1, k in -1:1
result = result .+ (i, j, k)::T
end
end
result
end

# Warmup
for i in 1:10
basic()
fancy(Tuple{Int64,Int64,Int64})
end

println("basic:")
println(@time basic())
println("fancy:")
println(@time fancy(Tuple{Int64,Int64,Int64}))
输出:
basic:
0.000000 seconds
(0, 0, 0)
fancy:
0.762599 seconds (10.80 M allocations: 494.385 MiB, 3.68% gc time)
(0, 0, 0)
如果我改写:
            result = result .+ (i, j, k)::T
我得到:
basic:
0.000000 seconds
(0, 0, 0)
fancy:
0.789512 seconds (13.50 M allocations: 576.782 MiB, 3.77% gc time)
(0, 0, 0)
有没有办法让 basicfancy执行相同的操作,即在 fancy 中没有分配?

最佳答案

只需更改 fancy 的签名即可:

function fancy(::Type{T}) where T
result::T = (0, 0, 0)
for i in 1:100000
for i in -1:1, j in -1:1, k in -1:1
result = result .+ (i, j, k)::T
end
end
result
end

println(@time fancy(Tuple{Int64,Int64,Int64}))
请注意,这两个函数实际上都优化为几乎无操作。您可以通过 @code_native basic() 检查它.

关于julia - 别名元组类型时的额外内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65351968/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com