基准测试函数名称后面的“-6”是什么意思?

我有一个go测试文件,我在其中编写了一个基准测试函数,如下所示:


func BenchmarkStuff(b *testing.B) {

    for i := 0; i < b.N; i++ {

        stuff()

    }

}

当我用 运行这个时,我得到这个:go test -benchtime 1x


BenchmarkStuff-6               1     847751900 ns/op

这是什么意思?这似乎不必要地隐晦。-6


jeck猫
浏览 112回答 1
1回答

陪伴而非守候

它表示用于运行基准测试的CPU数量 - 因此,在您的情况下。6编辑:Go 网站上似乎没有正式记录 Benchmark 名称格式,但您可以在标准库源代码中看到名称的制定方式:这里和这里:runtime.GOMAXPROCS(procs)benchName := benchmarkName(b.name, procs)func benchmarkName(name string, n int) string {&nbsp; &nbsp; if n != 1 {&nbsp; &nbsp; &nbsp; &nbsp; return fmt.Sprintf("%s-%d", name, n)&nbsp; &nbsp; }&nbsp; &nbsp; return name}仅供参考:从命令行文档:go helpgo help testflag&nbsp; &nbsp; -cpu 1,2,4&nbsp; &nbsp; &nbsp; &nbsp; Specify a list of GOMAXPROCS values for which the tests or&nbsp; &nbsp; &nbsp; &nbsp; benchmarks should be executed. The default is the current value&nbsp; &nbsp; &nbsp; &nbsp; of GOMAXPROCS.因此,如果您想强制基准测试使用较少数量的CPU,请使用env var:GOMAXPROCS$ GOMAXPROCS=2&nbsp; go test -bench=....BenchmarkStuff-2&nbsp; &nbsp; 1000000000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.2642 ns/op或者,您可以对多个 CPU 内核设置进行基准测试,如下所示:$ go test -bench=. -cpu=1,2,4...BenchmarkStuff&nbsp; &nbsp; &nbsp; 1000000000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.2516 ns/opBenchmarkStuff-2&nbsp; &nbsp; 1000000000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.2531 ns/opBenchmarkStuff-4&nbsp; &nbsp; 1000000000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.2488 ns/op
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go