Created
September 2, 2016 04:01
-
-
Save prashantv/d8773ac5fd8c6abab9ab1db23a0a8aff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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