Standard library / testing

testing.AllocsPerRun()

Measure average allocations for a function after a warm-up; runs must be positive. This fixture uses a no-op. Expected stdout: '0\nPASS\n'. Run as a _test.go file using the Go test harness.

Formal Syntax & Structure

go syntaxgo
func AllocsPerRun(runs int, f func()) (avg float64)

Parameters & Attributes

NameTypeDescription
runsintNumber of measured calls after warm-up.
ffunc()Function under test or callback to run.

Return Value

(avg float64)

Result of the operation described above.

Working Example

Example Snippetgo
package main
import (  "fmt"  "testing")
func TestReference(t *testing.T) { fmt.Println(testing.AllocsPerRun(5, func() {})) }
Go Reference