【问题标题】:Simple example of type unstable function in Julia 1.x with performance consequencesJulia 1.x 中类型不稳定函数的简单示例,具有性能影响
【发布时间】:2019-06-14 02:47:56
【问题描述】:

我发现的所有用于说明 Julia = 0.7 中不再有效,因为它们通常使用两种或几种可能的类型,但现在编译器在处理方面也很有效Union{T1,T2,..} 类型,因此与相应类型稳定版本的差异消失了。

你能举一个简单的类型不稳定函数的例子吗?当它变成类型稳定时,它仍然有很大的性能改进?

【问题讨论】:

    标签: types julia type-stability


    【解决方案1】:

    通常最显着的区别是全局变量引入类型不稳定性。下面是一个使用屏障函数消除类型不稳定性的例子:

    x = 10
    
    function f1()
        [i + x for i in 1:10^6]
    end
    
    function f2()
       h(x) = [i + x for i in 1:10^6]
       h(x)
    end
    

    这里是基准:

    julia> using BenchmarkTools
    
    julia> @benchmark f1()
    BenchmarkTools.Trial:
      memory estimate:  38.13 MiB
      allocs estimate:  1998992
      --------------
      minimum time:     30.133 ms (3.66% GC)
      median time:      33.713 ms (3.39% GC)
      mean time:        35.206 ms (5.35% GC)
      maximum time:     100.575 ms (50.58% GC)
      --------------
      samples:          142
      evals/sample:     1
    
    julia> @benchmark f2()
    BenchmarkTools.Trial:
      memory estimate:  7.63 MiB
      allocs estimate:  2
      --------------
      minimum time:     2.325 ms (0.00% GC)
      median time:      3.286 ms (0.00% GC)
      mean time:        3.725 ms (20.26% GC)
      maximum time:     52.838 ms (93.68% GC)
      --------------
      samples:          1342
      evals/sample:     1
    

    另一个常见的性能损失情况是当您使用抽象 eltype 的容器时(但我知道您没有将其归类为函数类型不稳定,因为它是数据类型不稳定)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 2016-08-14
      • 2020-12-24
      相关资源
      最近更新 更多