Go基准测试:ns/op和运行时间之间的不一致

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

Go benchmarking: dissonance between ns/op and runtime

问题

我正在对我在Go中创建的软件库进行基准测试,并且在运行时间和ns/op之间遇到了不一致的情况。我对基准测试还不熟悉,Go的文档和过去的stackoverflow问题在概念上没有详细介绍基准测试,所以我正在寻求比我更具概念性知识的人的帮助,以帮助我(以及其他处于类似困境的stackoverflow用户)理解到底发生了什么。

使用原生Go执行任务的基准测试输出:

1000000000               0.6136 ns/op          0 B/op          0 allocs/op
PASS
ok      github.com/gabetucker2/gostack/benchmark        0.862s

使用我的软件库执行相同任务的基准测试输出:

1576087               805.3 ns/op           544 B/op         21 allocs/op
PASS
ok      github.com/gabetucker2/gostack/benchmark        2.225s

注意两件事:

  • 我的软件库的ns/op比原生Go慢大约1200倍
  • 我的软件库的运行时间比原生Go慢大约2倍

对我来说,似乎很难相信我的软件库中一个非常简单的函数会比原生Go代码慢1200倍,而更有可能的是只慢2倍...所以到底发生了什么?

以防有用,这里是被调用的基准测试函数:

func test_Native_CreateArray() {

	myArr := []int {1, 2, 3}
	
	gogenerics.RemoveUnusedError(myArr)

}

func test_Gostack_CreateArray() {

	myStack := MakeStack([]int {1, 2, 3})
		
	gogenerics.RemoveUnusedError(myStack)

}

// 原生Go
func Benchmark_Native_CreateArray(b *testing.B) {
	for i := 0; i < b.N; i++ {
		test_Native_CreateArray()
    }
}
// 我的软件库"gostack"
func Benchmark_Gostack_CreateArray(b *testing.B) {
	for i := 0; i < b.N; i++ {
		test_Gostack_CreateArray()
    }
}

非常感谢任何帮助解答这个问题。

英文:

I am benchmarking a software library I created in Go, and I encountered dissonance between runtime and ns/op. I am new to benchmarking, and Go's documentation and past stackoverflow questions do not conceptually cover benchmarking in depth, so I am seeking someone with more conceptual knowledge than me to help me (and other stackoverflow users in similar predicaments) understand what exactly is happening.

Benchmarking output for a task performed using native Go:

1000000000               0.6136 ns/op          0 B/op          0 allocs/op
PASS
ok      github.com/gabetucker2/gostack/benchmark        0.862s

Benchmarking output for the same task performed using my software library:

1576087               805.3 ns/op           544 B/op         21 allocs/op
PASS
ok      github.com/gabetucker2/gostack/benchmark        2.225s

Notice two things:

  • The ns/op of my software library is around 1200 times slower than the ns/op of native Go
  • The runtime of my software library is around 2 times slower than the runtime of native Go

It seems impossible to me that a very simple function from my software library should be 1200 times slower than native Go code, and it seems much more plausible that it is only 2 times slower... so what exactly is going on here?

Just in case it is useful, here are the Benchmark functions being called:

func test_Native_CreateArray() {

	myArr := []int {1, 2, 3}
	
	gogenerics.RemoveUnusedError(myArr)

}

func test_Gostack_CreateArray() {

	myStack := MakeStack([]int {1, 2, 3})
		
	gogenerics.RemoveUnusedError(myStack)

}

// native Go
func Benchmark_Native_CreateArray(b *testing.B) {
	for i := 0; i &lt; b.N; i++ {
		test_Native_CreateArray()
    }
}
// my software library &quot;gostack&quot;
func Benchmark_Gostack_CreateArray(b *testing.B) {
	for i := 0; i &lt; b.N; i++ {
		test_Gostack_CreateArray()
    }
}

Any clarity would be greatly appreciated.

答案1

得分: 2

第一个函数运行了10亿次,每次耗时0.61纳秒,总运行时间为0.862秒。

第二个函数运行了1,576,087次,每次耗时805纳秒,占用了2.225秒的大约1.26875秒。如果强制第二个函数运行10亿次,预计需要的时间为805秒加上额外开销。

英文:

The first function ran 1_000_000_000 times with 0.61ns/op which is 0.61 seconds of the total runtime which took 0.862 seconds.

The second function ran 1_576_087 time with 805ns/op this takes around 1.26875 seconds of the 2.225 seconds. Forcing the second function to run 1_000_000_000 times should end up with around 805 seconds + overhead.

huangapple
  • 本文由 发表于 2022年10月27日 15:09:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/74218058.html
匿名

发表评论

匿名网友

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

确定