Create a gist now

Instantly share code, notes, and snippets.

@caike /main.go
Created May 24, 2016

Parallel example
package main
import (
"fmt"
"sync"
"time"
"math"
)
func main() {
//runtime.GOMAXPROCS(runtime.NumCPU())
//runtime.GOMAXPROCS(1)
startTime := time.Now().Unix()
names := []string{"Phil", "Noodles", "Barbaro"}
var wg sync.WaitGroup
wg.Add(len(names))
for _, name := range names {
go printName(name, &wg)
}
wg.Wait()
endTime := time.Now().Unix()
duration := endTime - startTime
fmt.Println("\nDone in", duration, "seconds")
}
func printName(n string, wg *sync.WaitGroup) {
result := 0.0
for i := 0; i < 100000000; i++ {
result += 3 / math.Pi * math.Sin(2)
}
fmt.Println("Name: ", n)
wg.Done()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment