Julia JIT编译、@时间和分配数

Julia JIT编译、@时间和分配数,julia,Julia,我刚刚开始评估Julia(版本0.6.0),并测试了如何调整大小!还有sizehint!可能会影响性能。我使用@time宏 文档中说“运行一次JIT编译”,但如果我们检查分配的数量,似乎运行一次是不够的 模块测试 函数测试(x::Int64;hint::Bool=false,resize::Bool=false) A::数组{Int64}=[] n::Int64=x 如果调整大小 调整大小!(A,n) 因为我在1:n A[i]=i 结束 其他的 如果提示sizehint!(A,n)结束 因为我在

我刚刚开始评估Julia(版本0.6.0),并测试了如何调整大小!还有sizehint!可能会影响性能。我使用@time宏

文档中说“运行一次JIT编译”,但如果我们检查分配的数量,似乎运行一次是不够的

模块测试
函数测试(x::Int64;hint::Bool=false,resize::Bool=false)
A::数组{Int64}=[]
n::Int64=x
如果调整大小
调整大小!(A,n)
因为我在1:n
A[i]=i
结束
其他的
如果提示sizehint!(A,n)结束
因为我在1:n
推(A,i)
结束
结束
A[完]
结束
结束
导入测试
#测试。测试(1);#(1)
#测试。测试(1,提示=true)#(2)
#Test.Test(1,resize=true)#(3)
@时间测试。测试(10_000_000)
@时间测试。测试(10_000_000,提示=true)
@时间测试。测试(10_000,resize=true)
对于不同的“JIT预编译”调用,我得到了不同的结果:

上述代码的结果:

    0.494120 seconds (11.02 k allocations: 129.706 MiB, 22.77% gc time)
    0.141155 seconds (3.43 k allocations: 76.537 MiB, 41.94% gc time)
    0.068319 seconds (9 allocations: 76.294 MiB, 76.99% gc time)
如果(1)未注释:

    0.520939 seconds (112 allocations: 129.007 MiB, 21.79% gc time)
    0.140845 seconds (3.43 k allocations: 76.537 MiB, 42.35% gc time)
    0.068741 seconds (9 allocations: 76.294 MiB, 77.55% gc time)
    0.586479 seconds (112 allocations: 129.007 MiB, 19.28% gc time)
    0.117521 seconds (9 allocations: 76.294 MiB, 50.56% gc time)
    0.068275 seconds (9 allocations: 76.294 MiB, 76.84% gc time)
    0.509668 seconds (112 allocations: 129.007 MiB, 21.61% gc time)
    0.112276 seconds (9 allocations: 76.294 MiB, 50.58% gc time)
    0.065123 seconds (9 allocations: 76.294 MiB, 76.34% gc time)
    0.497802 seconds (240 allocations: 129.016 MiB, 22.53% gc time)
    0.117035 seconds (11 allocations: 76.294 MiB, 52.56% gc time)
    0.067170 seconds (11 allocations: 76.294 MiB, 76.93% gc time)
如果(1)和(2)未注释:

    0.520939 seconds (112 allocations: 129.007 MiB, 21.79% gc time)
    0.140845 seconds (3.43 k allocations: 76.537 MiB, 42.35% gc time)
    0.068741 seconds (9 allocations: 76.294 MiB, 77.55% gc time)
    0.586479 seconds (112 allocations: 129.007 MiB, 19.28% gc time)
    0.117521 seconds (9 allocations: 76.294 MiB, 50.56% gc time)
    0.068275 seconds (9 allocations: 76.294 MiB, 76.84% gc time)
    0.509668 seconds (112 allocations: 129.007 MiB, 21.61% gc time)
    0.112276 seconds (9 allocations: 76.294 MiB, 50.58% gc time)
    0.065123 seconds (9 allocations: 76.294 MiB, 76.34% gc time)
    0.497802 seconds (240 allocations: 129.016 MiB, 22.53% gc time)
    0.117035 seconds (11 allocations: 76.294 MiB, 52.56% gc time)
    0.067170 seconds (11 allocations: 76.294 MiB, 76.93% gc time)
如果(1)和(2)和(3)未注释:

    0.520939 seconds (112 allocations: 129.007 MiB, 21.79% gc time)
    0.140845 seconds (3.43 k allocations: 76.537 MiB, 42.35% gc time)
    0.068741 seconds (9 allocations: 76.294 MiB, 77.55% gc time)
    0.586479 seconds (112 allocations: 129.007 MiB, 19.28% gc time)
    0.117521 seconds (9 allocations: 76.294 MiB, 50.56% gc time)
    0.068275 seconds (9 allocations: 76.294 MiB, 76.84% gc time)
    0.509668 seconds (112 allocations: 129.007 MiB, 21.61% gc time)
    0.112276 seconds (9 allocations: 76.294 MiB, 50.58% gc time)
    0.065123 seconds (9 allocations: 76.294 MiB, 76.34% gc time)
    0.497802 seconds (240 allocations: 129.016 MiB, 22.53% gc time)
    0.117035 seconds (11 allocations: 76.294 MiB, 52.56% gc time)
    0.067170 seconds (11 allocations: 76.294 MiB, 76.93% gc time)
如果(3)未注释:

    0.520939 seconds (112 allocations: 129.007 MiB, 21.79% gc time)
    0.140845 seconds (3.43 k allocations: 76.537 MiB, 42.35% gc time)
    0.068741 seconds (9 allocations: 76.294 MiB, 77.55% gc time)
    0.586479 seconds (112 allocations: 129.007 MiB, 19.28% gc time)
    0.117521 seconds (9 allocations: 76.294 MiB, 50.56% gc time)
    0.068275 seconds (9 allocations: 76.294 MiB, 76.84% gc time)
    0.509668 seconds (112 allocations: 129.007 MiB, 21.61% gc time)
    0.112276 seconds (9 allocations: 76.294 MiB, 50.58% gc time)
    0.065123 seconds (9 allocations: 76.294 MiB, 76.34% gc time)
    0.497802 seconds (240 allocations: 129.016 MiB, 22.53% gc time)
    0.117035 seconds (11 allocations: 76.294 MiB, 52.56% gc time)
    0.067170 seconds (11 allocations: 76.294 MiB, 76.93% gc time)
我的问题是:

  • 是虫子吗
  • 如果不是bug,那么是否有可能调用完整编译 不,文档明确指出这是由于您在全局范围内运行
    @time

    julia> function foo()
              Test.test(1)  # warm-up
              @time Test.test(10_000_000)
              @time Test.test(10_000_000, hint=true)
              @time Test.test(10_000_000, resize=true)
           end
    foo (generic function with 1 method)
    
    julia> foo()
      0.401256 seconds (26 allocations: 129.001 MiB, 47.38% gc time)
      0.185094 seconds (6 allocations: 76.294 MiB, 37.13% gc time)
      0.034649 seconds (6 allocations: 76.294 MiB, 30.99% gc time)
    

    感谢您快速而有用的回答!:)将测试明确地包含到功能中有助于解决问题。请您更具体一点,在doc清楚描述此行为的地方复制并粘贴单词好吗?在第二个代码块上方:“作为提示,此函数的改进版本不分配内存(下面报告的分配是由于在全局范围内运行@time宏),并且在第一次调用后执行速度加快了一个数量级:”