Skip to content

Instantly share code, notes, and snippets.

@caike
Created May 24, 2016 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caike/a1511443a347d8e034ddf38e46a997cc to your computer and use it in GitHub Desktop.
Save caike/a1511443a347d8e034ddf38e46a997cc to your computer and use it in GitHub Desktop.
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