matrix - 为什么在 Julia 0.5.0 中索引大型矩阵比 0.4.7 慢 170 倍?

标签 matrix julia

在 0.5 和 0.6 中索引大型矩阵似乎比 0.4.7 花费的时间长得多。

例如:

x = rand(10,10,100,4,4,1000)   #Dummy array

tic()
r = squeeze(mean(x[:,:,1:80,:,:,56:800],(1,2,3,4,5)),(1,2,3,4,5))
toc()

Julia 0.5.0 -> 耗时:176.357068283 秒

Julia 0.4.7 -> 耗时:1.19991952 秒


编辑:根据要求,我更新了基准以使用 BenchmarkTools.jl 并将代码包装在一个函数中:

using BenchmarkTools
function testf(x)
    r = squeeze(mean(x[:,:,1:80,:,:,56:800],(1,2,3,4,5)),(1,2,3,4,5));
end

x = rand(10,10,100,4,4,1000)   #Dummy array
@benchmark testf(x)

在 0.5.0 中,我得到以下信息(使用大量内存):

BenchmarkTools.Trial: 
  samples:          1
  evals/sample:     1
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  23.36 gb
  allocs estimate:  1043200022
  minimum time:     177.94 s (1.34% GC)
  median time:      177.94 s (1.34% GC)
  mean time:        177.94 s (1.34% GC)
  maximum time:     177.94 s (1.34% GC)

在 0.4.7 中我得到:

BenchmarkTools.Trial: 
  samples:          11
  evals/sample:     1
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  727.55 mb
  allocs estimate:  79
  minimum time:     425.82 ms (0.06% GC)
  median time:      485.95 ms (11.31% GC)
  mean time:        482.67 ms (10.37% GC)
  maximum time:     503.27 ms (11.22% GC)

编辑:更新为在 0.4.7 中使用 sub 并在 0.5.0 中使用 view

using BenchmarkTools
function testf(x)
    r = mean(sub(x, :, :, 1:80, :, :, 56:800));
end

x = rand(10,10,100,4,4,1000)   #Dummy array
@benchmark testf(x)

在 0.5.0 中,它运行了 >20 分钟并给出:

BenchmarkTools.Trial: 
  samples:          1
  evals/sample:     1
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  53.75 gb
  allocs estimate:  2271872022
  minimum time:     407.64 s (1.32% GC)
  median time:      407.64 s (1.32% GC)
  mean time:        407.64 s (1.32% GC)
  maximum time:     407.64 s (1.32% GC)

在 0.4.7 中我得到:

BenchmarkTools.Trial: 
  samples:          5
  evals/sample:     1
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  1.28 kb
  allocs estimate:  34
  minimum time:     1.15 s (0.00% GC)
  median time:      1.16 s (0.00% GC)
  mean time:        1.16 s (0.00% GC)
  maximum time:     1.18 s (0.00% GC)

这似乎在其他机器上可重复,因此已打开一个问题:https://github.com/JuliaLang/julia/issues/19174

最佳答案

编辑 2017 年 3 月 17 日 此回归已在 Julia v0.6.0 中修复。如果使用旧版本的 Julia,讨论仍然适用。

尝试在 Julia v0.4.7 和 v0.5.0 中运行这个原始脚本(将 sub 更改为 view):

using BenchmarkTools

function testf()
    # set seed
    srand(2016)

    # test array
    x = rand(10,10,100,4,4,1000)

    # extract array view
    y = sub(x, :, :, 1:80, :, :, 56:800)   # julia v0.4
    #y = view(x, :, :, 1:80, :, :, 56:800)  # julia v0.5

    # wrap mean(y) into a function
    z() = mean(y)

    # benchmark array mean
    @time z() 
    @time z() 
end

testf()

我的机器:

julia> versioninfo() 
Julia Version 0.4.7 
Commit ae26b25 (2016-09-18 16:17 UTC) 
Platform Info: 
  System: Darwin (x86_64-apple-darwin13.4.0) 
  CPU: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz 
  WORD_SIZE: 64 
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell) 
  LAPACK: libopenblas64_ 
  LIBM: libopenlibm 
  LLVM: libLLVM-3.3 

我的输出,Julia v0.4.7:

1.314966 seconds (246.43 k allocations: 11.589 MB)
1.017073 seconds (1 allocation: 16 bytes)

我的输出,Julia v0.5.0:

417.608056 seconds (2.27 G allocations: 53.749 GB, 0.75% gc time)
410.918933 seconds (2.27 G allocations: 53.747 GB, 0.72% gc time)

看来您可能发现了性能下降。考虑提交 issue .

关于matrix - 为什么在 Julia 0.5.0 中索引大型矩阵比 0.4.7 慢 170 倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40351485/

相关文章:

python - 将 scipy 稀疏行矩阵添加到另一个稀疏矩阵

matrix - 贝叶斯网络的混淆矩阵

r - 将距离矩阵转换并保存为特定格式

machine-learning - AdaGram.jl 上训练文本的问题

julia - 在虚拟环境中从终端运行 Julia 脚本

opengl - GL_PROJECTION矩阵或GL_MODELVIEW矩阵上的gluLookAt()最佳用法

julia - Julia中“==”和“===”比较运算符有什么区别?

julia - 为许多类似功能实现多次调度的有效方法

julia - 定义中的评估

r - 对数协方差到算术协方差矩阵函数?