基准测试Go代码和goroutine

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

Benchmark Go code and goroutines

问题

我想对一个名为test()的函数进行基准测试,使用不同数量的线程来执行它。

没有使用goroutines:

var t1 = time.Now()
test()
var elapsed1 = time.Since(t1)

> 1纳秒/操作

使用goroutines:

runtime.GOMAXPROCS(1)
var t1 = time.Now()
go test()
var elapsed1 = time.Since(t1)

> 1.10^-6纳秒/操作

我的测试函数:

func test() {
    for i := 0; i < 1000000000; i++ {
        float_result = f1 + f2
        float_result = f1 - f2
        float_result = f1 * f2
        float_result = f1 / f2
        float_result = f1 + f2
        float_result = f1 - f2
        float_result = f1 * f2
        float_result = f1 / f2
        float_result = f1 + f2
        float_result = f1 - f2
        float_result = f1 * f2
        float_result = f1 / f2
        float_result = f1 + f2
        float_result = f1 - f2
        float_result = f1 * f2
        float_result = f1 / f2
        float_result = f1 + f2
        float_result = f1 - f2
        float_result = f1 * f2
        float_result = f1 / f2
    }
}
  • 在这种情况下,使用goroutines对test()函数进行了良好的基准测试吗?
  • 如何才能达到0.001纳秒/操作?看起来速度太快了。(2.5GHz Intel Core i7)
  • 使用runtime.GOMAXPROCS(n)和使用n个线程是等效的吗?
英文:

I would like to benchmark a function: test(), with different numbers of threads working on it.

Without goroutines:

var t1 = time.Now()
test()
var elapsed1 = time.Since(t1)

> 1 ns / operation

With goroutines:

runtime.GOMAXPROCS(1)
var t1 = time.Now()
go test()
var elapsed1 = time.Since(t1)

> 1.10^-6 ns / operation

My test function:

func test() {
    for i := 0; i &lt; 1000000000; i++ {
	    float_result = f1 + f2
	    float_result = f1 - f2
	    float_result = f1 * f2
	    float_result = f1 / f2
	    float_result = f1 + f2
	    float_result = f1 - f2
    	float_result = f1 * f2
    	float_result = f1 / f2
     	float_result = f1 + f2
    	float_result = f1 - f2
    	float_result = f1 * f2
	    float_result = f1 / f2
	    float_result = f1 + f2
	    float_result = f1 - f2
	    float_result = f1 * f2
	    float_result = f1 / f2
	    float_result = f1 + f2
	    float_result = f1 - f2
	    float_result = f1 * f2
	    float_result = f1 / f2
    }
}
  • Is the test() function well benchmarked when I use goroutines in this case?
  • How is it possible to reach 0.001ns / operation? It looks to be way too fast. (2.5GHz Intel Core i7)
  • Is the use of goroutines with runtime.GOMAXPROCS(n) equivalent to use n threads ?

答案1

得分: 8

你没有测量test()运行的时间,而是测量了使用go test()调用/创建新goroutine所需的时间。

你需要等待goroutine完成,例如使用sync.WaitGroup

// 在你的main包中的某处
var wg sync.WaitGroup

func test() {
   // test()的第一行应该是
   wg.Add(1)
   defer wg.Done()
   ...
}


// 基准测试
runtime.GOMAXPROCS(1)
var t1 = time.Now()
go test()
wg.Wait()
var elapsed1 = time.Since(t1)
英文:

You are not measuring the time that test() runs, but the time it takes to call/create a new goroutine with go test().

You need to wait for your goroutine(s) to finish, for example by using a sync.Waitgroup.

// somewhere in your main package
var wg sync.WaitGroup

func test() {
   // first lines in test() should be
   wg.Add(1)
   defer wg.Done()
   ...
}


// benchmark
runtime.GOMAXPROCS(1)
var t1 = time.Now()
go test()
wg.Wait()
var elapsed1 = time.Since(t1)

答案2

得分: 1

你正在尝试对一个非常小的东西进行基准测试,以至于测量误差占主导地位。你应该调整基准测试的迭代次数,直到基准函数的持续时间足够长,可以可靠地计时。你的基准测试应该运行大约一秒钟才有意义。

英文:

You are tring to benchmark something so small that measurement error dominates. Your benchmark iterations should be adjusted until the benchmark function lasts long enough to be timed reliably.Your benchmark should run for about one second to be meaningful.

huangapple
  • 本文由 发表于 2016年2月6日 03:20:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/35231859.html
匿名

发表评论

匿名网友

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

确定