微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Julia - 使用 readdlm 会导致@code_warntype 性能不佳

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?