【问题标题】:How to measure time of julia program?如何测量 Julia 程序的时间?
【发布时间】:2020-12-05 13:45:42
【问题描述】:

如果我想计算 Julia 中的东西

invQa = ChebyExp(g->1/Q(g),0,1,5) 
a1Inf = ChebyExp(g->Q(g),1,10,5)
invQb = ChebyExp(g->1/Qd(g),0,1,5)
Qb1Inf = ChebyExp(g->Qd(g),1,10,5)

如何计算时间?我必须等待几秒钟才能完成这四件事?我是否将tic() 放在开头,toc() 放在最后?

我试过@elapsed,但没有结果。

【问题讨论】:

    标签: time julia


    【解决方案1】:

    基本的方法是使用

    @time begin
      #code
    end
    

    但请注意你never should benchmark in the global scope

    可以帮助您对代码进行基准测试的软件包是 BenchmarkTools.jl,您也应该查看它。

    【讨论】:

    • 这个最简单的版本大概是using BenchmarkTools; @btime begin...
    • 如果你这样做,你必须记住插入变量。
    • 特别是如果变量在全局范围内:joy:
    【解决方案2】:

    你可以这样做(我猜 g 是输入参数):

    function cheby_test(g::Your_Type)
        invQa = ChebyExp(g->1/Q(g),0,1,5) 
        a1Inf = ChebyExp(g->Q(g),1,10,5)
        invQb = ChebyExp(g->1/Qd(g),0,1,5)
        Qb1Inf = ChebyExp(g->Qd(g),1,10,5)
    end
    
    function test()
        g::Your_Type = small_quick  #  
        cheby_test(g)    #= function is compiled here and 
                            you like to exclude compile time from test =#
        g = real_data()
        @time cheby_test(g)  # here you measure time for real data
    end
    
    test()
    

    如果你喜欢时间宏中的to get proper allocation info,我建议不在全局范围内调用@time。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多