julia - 如何在 Julia 中进行正确的微基准测试?

标签 julia

Julia 1.0.0 documentation提供一般提示。

它还建议不要使用@time 宏:

For more serious benchmarking, consider the BenchmarkTools.jl package which among other things evaluates the function multiple times in order to reduce noise.

它们在使用上如何比较,是否值得使用“基础”Julia 中没有的东西?

最佳答案

从统计的角度来看,@benchmark 比@time 好很多

TL;DR BenchmarkTools @benchmark 宏是一个很棒的微基准测试工具。 谨慎使用 @time 宏,不要把第一次运行当回事。

这个简单的例子说明了使用和区别:

julia> # Fresh Julia 1.0.0 REPL

julia> # Add BenchmarkTools package using ] key package manager

(v1.0) pkg> add BenchmarkTools  
julia> # Press backspace key to get back to Julia REPL

# Load BenchmarkTools package into current REPL
julia> using BenchmarkTools

julia> # Definine a function with a known elapsed time
julia> f(n) = sleep(n)  # n is in seconds
f (generic function with 1 method)

# Expect just over 500 ms for elapsed time
julia> @benchmark f(0.5)
BenchmarkTools.Trial:
  memory estimate:  192 bytes
  allocs estimate:  5
  --------------
  minimum time:     501.825 ms (0.00% GC)
  median time:      507.386 ms (0.00% GC)
  mean time:        508.069 ms (0.00% GC)
  maximum time:     514.496 ms (0.00% GC)
  --------------
  samples:          10
  evals/sample:     1

julia> # Try second run to compare consistency
julia> # Note the very close consistency in ms for both median and mean times

julia> @benchmark f(0.5)
BenchmarkTools.Trial:
  memory estimate:  192 bytes
  allocs estimate:  5
  --------------
  minimum time:     502.603 ms (0.00% GC)
  median time:      508.716 ms (0.00% GC)
  mean time:        508.619 ms (0.00% GC)
  maximum time:     515.602 ms (0.00% GC)
  --------------
  samples:          10
  evals/sample:     1


julia> # Define the same function with new name for @time macro tests
julia> g(n) = sleep(n)
g (generic function with 1 method)

# First run suffers from compilation time, so 518 ms
julia> @time sleep(0.5)
  0.517897 seconds (83 allocations: 5.813 KiB)

# Second run drops to 502 ms, 16 ms drop
julia> @time sleep(0.5)
  0.502038 seconds (9 allocations: 352 bytes)

# Third run similar to second
julia> @time sleep(0.5)
  0.503606 seconds (9 allocations: 352 bytes)

# Fourth run increases over second by about 13 ms
julia> @time sleep(0.5)
  0.514629 seconds (9 allocations: 352 bytes)

这个简单的例子说明了使用 @benchmark 宏是多么容易,以及应该谨慎对待 @time 宏结果。

是的,使用 @benchmark 宏是值得的。

关于julia - 如何在 Julia 中进行正确的微基准测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52432895/

相关文章:

function - Julia 函数中的内存分配

julia - 为什么变量可以在 Julia 宏名称修改中以 # 开头?

Julia 宏无法识别数组参数

julia - 在 Julia 中模仿 Scala Option[T] 的最佳方式?

julia - 为值指定颜色

julia - Julia 非线性最小二乘包中的 Levenberg-Marquardt

sorting - Julia:按第 2 列然后按第 3 列对矩阵进行排序

julia - 为什么加号运算符在 Julia 中被矢量化而不是除法运算符?

performance - Julia 与 MATLAB : Why is my Julia code so slow?

iterator - 是否有用于从 julia 中的类似生成器的函数创建快速迭代器的宏?