Skip to content

Instantly share code, notes, and snippets.

@1a1a11a
Last active March 10, 2019 00:27
Show Gist options
  • Save 1a1a11a/a1b95f1a4939bf9480bbc957e6c17f71 to your computer and use it in GitHub Desktop.
Save 1a1a11a/a1b95f1a4939bf9480bbc957e6c17f71 to your computer and use it in GitHub Desktop.
some benchmark related to golang
package main
import (
"fmt"
"time"
"sync"
)
func main() {
wg := sync.WaitGroup{}
x := 0
channel := make(chan int, 20000000)
mtx := sync.Mutex{}
for i := 0; i < 1000000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
mtx.Lock()
x++
mtx.Unlock()
channel <- x
}()
}
wg.Wait()
fmt.Println("x ", x)
}
@1a1a11a
Copy link
Author

1a1a11a commented Mar 10, 2019

On a 2015 MacBook Pro 15,

  • without lock (of course, result not correct), the performance of creating 1000000 goroutines is around 0.5s.
  • with lock, the performance is even slightly better, it is also around 0.5s.
  • adding channel, the run time will increase to 0.8s.
  • adding both channel and mtx, the performance will increase to 1s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment