在Python中,评估代码性能的一种方便方法是使用timeit
。在Julia中,我们有@time
,但它的缺点是仅运行一次代码片段,这意味着您需要多次执行它才能获得关于代码性能的好的评估。有没有更好的方法来计时Julia的代码,类似于Python的timeit
?
软件包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)