Created
May 24, 2016 17:58
-
-
Save caike/a1511443a347d8e034ddf38e46a997cc to your computer and use it in GitHub Desktop.
Parallel example
This file contains 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" | |
"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