What does allocs/op and B/op mean in go benchmark?

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

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

根据我的理解:

  1. 10000 是迭代次数 for i := 0; i < b.N; i++ {
  2. XXX ns/op 是每次迭代完成所花费的大致时间。

但是,即使在阅读了文档之后,我仍然无法找到B/opallocs/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:

  1. 10000 is the number of iterations for i := 0; i &lt; b.N; i++ {.
  2. 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.

huangapple
  • 本文由 发表于 2016年2月24日 05:24:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/35588474.html
匿名

发表评论

匿名网友

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

确定