Skip to content

Instantly share code, notes, and snippets.

@prashantv
Created September 2, 2016 04:01
Show Gist options
  • Save prashantv/d8773ac5fd8c6abab9ab1db23a0a8aff to your computer and use it in GitHub Desktop.
Save prashantv/d8773ac5fd8c6abab9ab1db23a0a8aff to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync"
"sync/atomic"
"time"
)
func main() {
const (
numGoroutines = 4
numIterations = 100000000
)
var (
waitAllStart sync.WaitGroup
running sync.WaitGroup
n int32
)
perOpTimes := make([]int64, numGoroutines)
waitAllStart.Add(numGoroutines)
running.Add(numGoroutines)
for i := 0; i < numGoroutines; i++ {
go func(g int) {
defer running.Done()
waitAllStart.Done()
waitAllStart.Wait()
started := time.Now()
for i := 0; i < numIterations; i++ {
atomic.AddInt32(&n, 1)
}
total := time.Since(started)
perOpTimes[g] = total.Nanoseconds() / numIterations
}(i)
}
running.Wait()
fmt.Println("Per op (ns):", perOpTimes)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment