如何测量Julia程序的运行时间?

8

如果我想在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,但没有结果。

2个回答

22

最简单的版本可能是 using BenchmarkTools; @btime begin... - Michael K. Borregaard
如果你这样做,你必须记得插值变量。 - Chris Rackauckas
特别是如果变量在全局范围内 :joy: - Michael K. Borregaard

5
你可以这样做(我猜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()

我建议如果你想从时间宏中获取正确的分配信息,就不要在全局范围内调用@time。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接