英文:
What does allocs/op and B/op mean in go benchmark?
问题
当我运行go test -v -bench=. -benchmem
来进行基准测试时,我看到以下结果。
f1 10000 120860 ns/op 2433 B/op 28 allocs/op
f2 10000 120288 ns/op 2288 B/op 26 allocs/op
根据我的理解:
10000
是迭代次数for i := 0; i < b.N; i++ {
。XXX ns/op
是每次迭代完成所花费的大致时间。
但是,即使在阅读了文档之后,我仍然无法找到B/op
和allocs/op
的含义。
我的猜测是allocs/op
与垃圾回收和内存分配有关(数值越小越好)。
有人能够对这些值的含义给出一个清晰的解释吗?同时,了解如何减少这些值的方法也会很有帮助(我意识到这是针对测试的,但可能有一些通用的提示适用于许多情况)。
英文:
When I run my benchmarks with go test -v -bench=. -benchmem
, I see the following results.
f1 10000 120860 ns/op 2433 B/op 28 allocs/op
f2 10000 120288 ns/op 2288 B/op 26 allocs/op
Based on my understanding:
10000
is the number of iterationsfor i := 0; i < b.N; i++ {
.XXX ns/op
is approximate time it took for one iteration to complete
But even after reading the docs, I can not find out what B/op
and allocs/op
mean.
My guess is that allocs/op has something to do with garbage collection and memory allocation (the less the better).
Can anyone give a nice explanation of the meaning of these values. Also it would be nice to know why do the go up and main steps to reduce them (I realize this is test specific, but may be there are some universal hints that work in many cases)
答案1
得分: 64
allocs/op
表示每个操作(单次迭代)发生了多少个不同的内存分配。
B/op
表示每个操作分配了多少字节。
英文:
allocs/op
means how many distinct memory allocations occurred per op (single iteration).
B/op
is how many bytes were allocated per op.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论