Julia JIT编译,@ time和分配数 [英] Julia JIT compilation, @time and number of allocations

查看:232
本文介绍了Julia JIT编译,@ time和分配数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始评估Julia(0.6.0版),并且测试了调整大小的方法!和sizehint!可能会影响性能.我使用了@time宏.

I am just starting to evaluate Julia (version 0.6.0) and I tested how resize! and sizehint! could impact performance. I used @time macro.

文档显示#运行一次以进行JIT编译",但是如果我们检查分配数,似乎仅运行一次是不够的.

Documentation says "# Run once to JIT-compile" but it seems that running once could not be enough if we check number of allocations.

module Test

function test(x::Int64; hint::Bool=false, resize::Bool=false)
    A::Array{Int64} = []
    n::Int64 = x
    if resize
        resize!(A, n)
        for i in 1:n
            A[i]=i
        end
    else
        if hint sizehint!(A, n) end
        for i in 1:n
            push!(A, i)
        end
    end
    A[end]
end

end

import Test

#Test.test(1);  # (1)
#Test.test(1, hint=true);  # (2)
#Test.test(1, resize=true);  # (3)
@time Test.test(10_000_000)
@time Test.test(10_000_000, hint=true)
@time Test.test(10_000_000, resize=true)

对于不同的"JIT预编译"调用,我得到了不同的结果:

I got different results for different "JIT-precompile" callings:

以上代码的结果:

    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)

如果(1)&& (2)没有评论:

if (1) && (2) are uncommented:

    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)

如果(1)&& (2)&& (3)没有评论:

if (1) && (2) && (3) are uncommented:

    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)

如果(3)未注释:

    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. 如果不是bug,那么有可能调用完整的编译吗?

推荐答案

不,文档

No, the doc here clearly tells this is due to you were running @time in global scope:

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)

这篇关于Julia JIT编译,@ time和分配数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆