有没有Julia相当于Python的%timeit

Yly*_*Yly 6 profiling julia

在Python中,评估代码性能的便捷方法是timeit.在Julia中我们有@time,但是它的缺点是只运行一段代码,这意味着你需要多次执行它才能获得良好的代码性能.在Julia中有更好的时间编码方式,更类似于Python timeit吗?

Chr*_*kas 15

该包BenchmarkTools具有@benchmark@btime具有确定的运行次数的统计方法.

julia> A = rand(100,100);
julia> B = rand(100,100);

julia> using BenchmarkTools

julia> @benchmark A*B
BenchmarkTools.Trial:
  memory estimate:  78.20 KiB
  allocs estimate:  2
  --------------
  minimum time:     48.302 ?s (0.00% GC)
  median time:      72.015 ?s (0.00% GC)
  mean time:        74.314 ?s (6.52% GC)
  maximum time:     3.232 ms (95.17% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @btime A*B;
  49.180 ?s (2 allocations: 78.20 KiB)
Run Code Online (Sandbox Code Playgroud)