Return values from BenchmarkTools @benchmark 从BenchmarkTools @benchmark返回的数值。

huangapple go评论61阅读模式
英文:

Return values from BenchmarkTools @benchmark

问题

我想收集我正在进行基准测试的函数的返回值信息,类似于

function foo(x)
    x + 1
end

results = []
@benchmark y = foo(x) setup = (x = 3) teardown = (push!(results, y))

错误:未定义变量 y

如何正确做到这一点?显然,在 foo 内部使用副作用会扭曲基准测试结果。

英文:

I would like to collect information about the return values of functions I'm benchmarking, akin to

function foo(x)
    x + 1
end

results = []
@benchmark y = foo(x) setup = (x = 3) teardown = (push!(results, y))

> ERROR: UndefVarError: y not defined

How do I do this properly? Using side-effects inside foo obviously distorts the benchmark.

答案1

得分: 2

问题在于基准测试的代码是由@benchmark在本地范围内运行的,因此y(在y = foo(x)中)只在该范围内作为本地变量被创建。teardown在代码执行完成后运行,无法访问该范围 - 此时y超出范围并且可能已经被释放。修复方法非常简单 - 将y标记为全局变量。

@benchmark (global y = foo(x)) setup = (x = 3) teardown = (push!(results, y)) 
英文:

The issue is that the benchmarked code is run in a local scope by @benchmark, and so y (in y = foo(x)) gets created only as a local variable within that scope. teardown runs after the code has finished executing, and doesn't have access to that scope - y is out of scope and possibly out of memory by then.

The fix is pretty simple - mark the y as a global variable.

@benchmark (global y = foo(x)) setup = (x = 3) teardown = (push!(results, y)) 

答案2

得分: 1

另一种可能的方法是使用引用:

y2 = Ref{Int}()
@benchmark y2[] = foo(x) setup = (x = 3) teardown = (push!(results, y2[]))

这将获取所有在results向量中收集的数据。

注意,这假定了y2的类型。
如果你不知道期望的类型,可以使用y2 = Ref{Any}()

英文:

Another possible way is to use a reference:

y2 = Ref{Int}()
@benchmark y2[] = foo(x) setup = (x = 3) teardown = (push!(results, y2[]))

This will get all data collected in the results vector.

Note this assumed the type for y2.
If you do not know what expect you could use y2 = Ref{Any}() instead.

huangapple
  • 本文由 发表于 2023年5月17日 20:38:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定