由于类型不稳定导致的性能差异?

时间:2015-07-26 02:46:46

标签: julia

我在Julia 0.4-prerelease中尝试以下代码,它以两种不同的方式执行矩阵求幂(精确vs系列扩展)。我尝试使用多种方法来获取数组维n并设置单位矩阵eye( n )

function test()
    A = [ 1.0 -1.0 ; -1.0  1.0 ]

    lam, U = eig( A )                         # diagonalization: A U = U diagm(lam)

    Bref = U * diagm( exp(lam) ) * U'         # Bref = exp(A) (in matrix sense)

    #[ Get the dimension n ]                            
    n = length( lam )                                                  # slow (1a)
    # const n = length( lam )                                          # slow (1b)
    # n::Int = length( lam )                                           # fast (1c)
    # const n::Int = length( lam )                                     # fast (1d)
    # n = size( A, 1 )                                                 # fast (1e)

    #[ Set unit matrices to B and X ]
    B = eye( n ); X = eye( n )                                         # slow with (1a) (2-1)
    # B = eye( 2 ); X = eye( 2 )                                       # fast (2-2)
    # B = eye( n::Int ); X = eye( n::Int )                             # fast (2-3) 
    # B::Array{Float64,2} = eye( n ); X::Array{Float64,2} = eye( n )   # fast (2-4)
    # B = eye( A ); X = eye( A )                                       # fast (2-5)

    #[ Calc B = exp(A) with Taylor expansion ]
    @time for k = 1:20
        X[:,:] = X * A / float( k )
        B[:,:] += X
    end

    #[ Check error ]
    @show norm( B - Bref )
end

test()

这里我观察到当n是动态变量(没有类型注释)时,代码变得比其他情况慢得多。例如,(1a)和(2-1)的组合给出 "慢"结果如下,而其他组合给出了快速"结果(超过1000倍)。

slow case => elapsed time: 0.043822985 seconds (1 MB allocated)
fast case => elapsed time: 1.1702e-5 seconds (16 kB allocated)

这是因为"类型不稳定"在for-loop里面发生?我感到困惑,因为eye( n )总是Array{Float64,2}(仅用于初始化),似乎没有(隐式)类型的更改。同样令人困惑的是(1e)和(2-1)的组合很快,其中动态n设置为size()而不是length()。总的来说,为了获得良好的性能,明确地注释数组维度变量是否更好?

1 个答案:

答案 0 :(得分:5)

我认为差异主要来自编译时间。如果我再放两个test(),我会得到以下内容:

2-11a

  73.599 milliseconds (70583 allocations: 3537 KB)
norm(B - Bref) = 4.485301019485633e-14
  15.165 microseconds (200 allocations: 11840 bytes)
norm(B - Bref) = 4.485301019485633e-14
  10.844 microseconds (200 allocations: 11840 bytes)
norm(B - Bref) = 4.485301019485633e-14

2-21a

   8.662 microseconds (180 allocations: 11520 bytes)
norm(B - Bref) = 4.485301019485633e-14
   7.968 microseconds (180 allocations: 11520 bytes)
norm(B - Bref) = 4.485301019485633e-14
   7.654 microseconds (180 allocations: 11520 bytes)
norm(B - Bref) = 4.485301019485633e-14

编译时间的差异来自于编译的不同代码。那个,以及剩下的一些时间差异,实际上来自于类型的不稳定性。查看@code_warntype test()版本1a的这一部分:

  GenSym(0) = (Base.LinAlg.__eig#214__)(GenSym(19),A::Array{Float64,2})::Tuple{Any,Any}
  #s8 = 1
  GenSym(22) = (Base.getfield)(GenSym(0),1)::Any
  GenSym(23) = (Base.box)(Base.Int,(Base.add_int)(1,1)::Any)::Int64
  lam = GenSym(22)
  #s8 = GenSym(23)
  GenSym(24) = (Base.getfield)(GenSym(0),2)::Any
  GenSym(25) = (Base.box)(Base.Int,(Base.add_int)(2,1)::Any)::Int64
  U = GenSym(24)
  #s8 = GenSym(25) # line 7:
  Bref = U * (Main.diagm)((Main.exp)(lam)::Any)::Any * (Main.ctranspose)(U)::Any::Any # line 9:
  n = (Main.length)(lam)::Any # line 11:
  B = (Main.eye)(n)::Any # line 11:
  X = (Main.eye)(n)::Any # line 13: # util.jl, line 170:

我读到这是因为类型推断未能找出eig的返回类型。然后传播到B和X.如果添加n::Int,最后一行将变为

  n = (top(typeassert))((top(convert))(Main.Int,(Main.length)(lam)::Any)::Any,Main.Int)::Int64 # line 11:
  B = (Base.eye)(Base.Float64,n::Int64,n::Int64)::Array{Float64,2} # line 11:
  X = (Base.eye)(Base.Float64,n::Int64,n::Int64)::Array{Float64,2} # line 13: # util.jl, line 170:

因此BX输入正确。 An issue about this exact subject was raised recently - 如果您希望获得最佳效果,除了自己注释之外,它似乎并没有多少选择。