英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论