57

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 < 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)

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753

1 Answers1

64

allocs/op means how many distinct memory allocations occurred per op (single iteration).

B/op is how many bytes were allocated per op.

Not_a_Golfer
  • 47,012
  • 14
  • 126
  • 92
  • Is that B/op the allocations, made in stake frame or in heap? Or is it what the memory amount that GO GC has to clean? – user3219492 Aug 04 '17 at 19:45
  • 4
    Is there a formal documentation to explain these metrics? – Ryan Lyu Jul 01 '19 at 02:40
  • What does `memory allocation` mean? Does that mean `set a value for a variable`? – Ryan Lyu Jul 01 '19 at 04:41
  • 2
    @RyanLv: No, it means memory was _allocated_, not modified. Setting a value for a variable just modifies the contents of pre-allocated memory. – Jonathan Hall Jul 01 '19 at 11:45
  • 5
    @RyanLv memory allocation means getting more memory from the operating system. Doing so reduces the available memory to your and other programs in your system. – Inanc Gumus Oct 26 '19 at 09:41