Skip to content

Instantly share code, notes, and snippets.

@jgrahamc
Created March 27, 2013 09:40
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jgrahamc/5253020 to your computer and use it in GitHub Desktop.
Save jgrahamc/5253020 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"runtime"
"time"
)
func waitAround(die chan bool) {
<- die
}
func main() {
cpus := runtime.NumCPU()
runtime.GOMAXPROCS(cpus)
count := 100000
die := make(chan bool)
var startMemory runtime.MemStats
runtime.ReadMemStats(&startMemory)
start := time.Now()
for i := 0; i < count; i++ {
go waitAround(die)
}
elapsed := time.Since(start)
var endMemory runtime.MemStats
runtime.ReadMemStats(&endMemory)
fmt.Printf("Started %d goroutines on %d CPUs in %f seconds\n",
count, cpus, elapsed.Seconds())
fmt.Printf("Memory before %d, memory after %d\n", startMemory.Alloc,
endMemory.Alloc)
fmt.Printf("%d goroutines running\n", runtime.NumGoroutine())
close(die)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment