如何解决Julia - 使用 readdlm 会导致@code_warntype 性能不佳
来自 Julia's documentation 的以下代码给出了 @code_warntype
红色性能(联合):
julia> using DelimitedFiles
julia> x = [1; 2; 3; 4];
julia> y = [5; 6; 7; 8];
julia> open("delim_file.txt","w") do io
writedlm(io,[x y])
end
julia> readdlm("delim_file.txt",'\t',Int,'\n')
4×2 Array{Int64,2}:
1 5
2 6
3 7
4 8
julia> @code_warntype readdlm("delim_file.txt",'\n')
Variables
#self#::Core.Compiler.Const(DelimitedFiles.readdlm,false)
input::String
dlm::Char
T::Core.Compiler.Const(Int64,false)
eol::Char
Body::Union{Tuple{Array{_A,2} where _A,Array{AbstractString,2}},Array{_A,2} where _A}
1 ─ %1 = Core.NamedTuple()::Core.Compiler.Const(NamedTuple(),false)
│ %2 = Base.pairs(%1)::Core.Compiler.Const(Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}(),false)
│ %3 = DelimitedFiles.:(var"#readdlm#6")(%2,#self#,input,dlm,T,eol)::Union{Tuple{Array{_A,2} where _A}
└── return %3
我有一个使用相同方案的代码,并且在逻辑上返回了同样糟糕的 code_warntype
性能。我找不到要更改的内容来改进我的代码。谢谢你帮助我!
function create_instance(filename)
"""
This function creates instance from filename.
Example of instance of 4 nodes in filename
4
1 0.5 0.5
2 0 0
3 1 1
4 -2 4
"""
data = readdlm(filename,' ',Float64,'\n')
n = Int(data[1,1])
x_coors = data[2:n+1,2]
y_coors = data[2:n+1,3]
ring_costs = zeros(Float64,n,n)
for i in 1:n
for j in 1:n
ring_costs[i,j] = dist([x_coors[i],y_coors[i]],[x_coors[j],y_coors[j]])
end
end
return n,ring_costs
end
dist(x,y) = sqrt((x[1]-y[1])^2 + (x[2]-y[2])^2)
解决方法
您可以做的一件事是使用 function barrier 以防止 readdlm
的类型不稳定传播到您的其余代码。
类似于:
# The user-facing function
function create_instance(filename)
"""
This function creates instance from filename.
Example of instance of 4 nodes in filename
4
1 0.5 0.5
2 0 0
3 1 1
4 -2 4
"""
data = readdlm(filename,' ',Float64,'\n')
create_instance_(data)
end
# An inner function introducing a function barrier
# When this function is called,the concrete type of `data` will be known,and
# everything can be compiled efficiently.
function create_instance_(data)
n = Int(data[1,1])
x_coors = data[2:n+1,2]
y_coors = data[2:n+1,3]
ring_costs = zeros(Float64,n,n)
for i in 1:n
for j in 1:n
ring_costs[i,j] = dist([x_coors[i],y_coors[i]],[x_coors[j],y_coors[j]])
end
end
return n,ring_costs
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。