This site is being rebuilt and some pages are out of date. For current details, write to [email protected]. This notice goes away when the rebuild is done.

No analytics unless you allow it, no tracking. This site keeps in your browser the language you pick, the theme, its colour, which site you chose, the currency on the pricing page and that you closed this notice; signing in adds session cookies. The legal page has the details.

Sign in

Radar · Go · Archive · Week 14 · Mar 31 – Apr 6, 2025

More predictable benchmarking with testing.B.Loop

Nice to knowstdlib

What changed
The testing.B.Loop helper provides a more deterministic benchmark loop in Go 1.24.
Production impact
The source does not say.
Try it
Create a benchmark that uses b.Loop() instead of a for loop.
Source
go.dev/blog/testing-b-loop

Understand it, then run it

Run it now

Todaygo
// This program demonstrates that the `testing.B.Loop` helper is available
// in Go 1.27.1. It does not run a benchmark; it simply compiles a
// benchmark function that uses `b.Loop()` and prints a confirmation.
package main

import (
	"fmt"
	"testing"
)

// BenchmarkExample shows the new `b.Loop()` syntax. It is not executed
// here, but the presence of this function proves that the API exists.
func BenchmarkExample(b *testing.B) {
	for b.Loop() {
		// The body would contain the code to benchmark.
	}
}

func main() {
	fmt.Println("testing.B.Loop is available in Go 1.27.1")
}

What it printed when we ran it on Go 1.27.1

testing.B.Loop is available in Go 1.27.1

Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.

Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed