如何在 Julia 中获取函数的执行时间?

Qwe*_*rty 8 julia

我想在 Julia 中获取函数的执行时间。这是一个最小的工作示例:

function raise_to(n)
    for i in 1:n
        y = (1/7)^n
    end
end
Run Code Online (Sandbox Code Playgroud)

如何获得执行所需的时间raise_to(10)

ffe*_*tte 10

对函数进行基准测试的推荐方法是使用BenchmarkTools

julia> function raise_to(n)
           y = (1/7)^n
       end
raise_to (generic function with 1 method)

julia> using BenchmarkTools

julia> @btime raise_to(10)
  1.815 ns (0 allocations: 0 bytes)
Run Code Online (Sandbox Code Playgroud)

请注意,多次重复计算(就像您在示例中所做的那样)是获得更准确测量值的好主意。但是BenchmarTools为你做。

另请注意,BenchmarkTools避免了仅使用@time. 最值得注意的是@time,除了运行时间之外,您还可能测量编译时间。这就是为什么第一次调用@time通常会显示更大的时间/分配:

# First invocation: the method gets compiled
# Large resource consumption
julia> @time raise_to(10)
  0.007901 seconds (7.70 k allocations: 475.745 KiB)
3.5401331746414338e-9

# Subsequent invocations: stable and low timings
julia> @time raise_to(10)
  0.000003 seconds (5 allocations: 176 bytes)
3.5401331746414338e-9

julia> @time raise_to(10)
  0.000002 seconds (5 allocations: 176 bytes)
3.5401331746414338e-9

julia> @time raise_to(10)
  0.000001 seconds (5 allocations: 176 bytes)
3.5401331746414338e-9
Run Code Online (Sandbox Code Playgroud)


phy*_*att 5

@时间

@time 如前面的答案中提到的那样工作,但如果这是您第一次在 julia 会话中调用该函数,它将包括编译时间。

https://docs.julialang.org/en/v1/manual/performance-tips/#Measure-performance-with-%5B%40time%5D%28%40ref%29-and-pay-attention-to-memory-分配-1

@btime

@btime如果您using BenchmarkTools输入代码,您也可以使用。

https://github.com/JuliaCI/BenchmarkTools.jl

这将在初始编译运行后多次重新运行您的函数,然后平均时间。

julia> using BenchmarkTools
julia> @btime sin(x) setup=(x=rand())
  4.361 ns (0 allocations: 0 bytes)
0.49587200950472454
Run Code Online (Sandbox Code Playgroud)

@timeit

另一个非常有用的分析库是 TimerOutputs.jl

https://github.com/KristofferC/TimerOutputs.jl

using TimerOutputs

# Time a section code with the label "sleep" to the `TimerOutput` named "to"
@timeit to "sleep" sleep(0.02)

# ... several more calls to @timeit

print_timer(to::TimerOutput)
Run Code Online (Sandbox Code Playgroud)
 ??????????????????????????????????????????????????????????????????????
                               Time                   Allocations
                       ??????????????????????   ???????????????????????
   Tot / % measured:        5.09s / 56.0%            106MiB / 74.6%

 Section       ncalls     time   %tot     avg     alloc   %tot      avg
 ??????????????????????????????????????????????????????????????????????
 sleep            101    1.17s  41.2%  11.6ms   1.48MiB  1.88%  15.0KiB
 nest 2             1    703ms  24.6%   703ms   2.38KiB  0.00%  2.38KiB
   level 2.2        1    402ms  14.1%   402ms      368B  0.00%   368.0B
   level 2.1        1    301ms  10.6%   301ms      368B  0.00%   368.0B
 throwing           1    502ms  17.6%   502ms      384B  0.00%   384.0B
 nest 1             1    396ms  13.9%   396ms   5.11KiB  0.01%  5.11KiB
   level 2.2        1    201ms  7.06%   201ms      368B  0.00%   368.0B
   level 2.1        3   93.5ms  3.28%  31.2ms   1.08KiB  0.00%   368.0B
 randoms            1   77.5ms  2.72%  77.5ms   77.3MiB  98.1%  77.3MiB
 funcdef            1   2.66?s  0.00%  2.66?s         -  0.00%        -
 ??????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

宏可以有begin...end

正如这些函数的文档中所见,它们可以涵盖多个语句或函数。

@my_macro begin
  statement1
  statement2
  # ...
  statement3
end
Run Code Online (Sandbox Code Playgroud)

希望有帮助。