How do i write a benchmark script in Go to measure ops/sec

半腔热情 提交于 2019-12-06 15:57:26

This is best handled by the built-in testing.Benchmark system. For example, a test case like:

func BenchmarkHello(b *testing.B) {
    for i := 0; i < b.N; i++ {
        fmt.Sprintf("hello")
    }
}

will automatically work out timing calculations for this function. See the package documentation for more details on setting it up, but it should be very familiar if you've written Go test cases before.

To your question about concurrency benchmarking, see the RunParallel helper, which is specifically to assist in this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!