Skip to content

Instantly share code, notes, and snippets.

@nobonobo
Created February 18, 2016 02:59
Show Gist options
  • Select an option

  • Save nobonobo/48e76b67c23f741652b4 to your computer and use it in GitHub Desktop.

Select an option

Save nobonobo/48e76b67c23f741652b4 to your computer and use it in GitHub Desktop.
バージョン別にgoroutineの中の処理が始まる遅延とgoステートメントの次の処理が始まる遅延を測ってみた。
package main
import (
"fmt"
"sync"
"time"
)
func BenchmarkGo(n int) time.Duration {
ch := make(chan time.Duration, n)
wg := sync.WaitGroup{}
wg.Add(n)
for i := 0; i < n; i++ {
now := time.Now()
go func(t time.Time) {
d := time.Since(t)
ch <- d
wg.Done()
time.Sleep(1000 * time.Millisecond)
}(now)
}
wg.Wait()
close(ch)
tms := time.Duration(0)
for d := range ch {
tms += d
}
return time.Duration(int(tms) / n)
}
func BenchmarkGoNext(n int) time.Duration {
tms := time.Duration(0)
for i := 0; i < n; i++ {
now := time.Now()
go func(t time.Time) {
time.Sleep(100 * time.Millisecond)
}(now)
tms += time.Since(now)
}
return time.Duration(int(tms) / n)
}
const N = 1000
func main() {
time.Sleep(200 * time.Millisecond)
fmt.Println("goroutine start deley:", BenchmarkGo(N))
time.Sleep(200 * time.Millisecond)
fmt.Println("goroutine next deley:", BenchmarkGoNext(N))
}
Now using version go1.4.3> go run goroutinebench.go
goroutine start deley: 818.705µs
goroutine next deley: 749ns
Now using version go1.5.3> go run goroutinebench.go
goroutine start deley: 50.511µs
goroutine next deley: 1.067µs
Now using version go1.6> go run goroutinebench.go
goroutine start deley: 75.578µs
goroutine next deley: 916ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment