forked from unknwon/the-way-to-go_ZH_CN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrent_pi.go
executable file
·45 lines (41 loc) · 1.01 KB
/
concurrent_pi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Concurrent computation of pi.
// See http://goo.gl/ZuTZM. - Comparison with Scala!
//
// This demonstrates Go's ability to handle
// large numbers of concurrent processes.
// It is an unreasonable way to calculate pi.
package main
import (
"fmt"
"math"
"time"
)
func main() {
start := time.Now()
fmt.Println(CalculatePi(5000))
end := time.Now()
delta := end.Sub(start)
fmt.Printf("longCalculation took this amount of time: %s\n", delta)
}
// CalculatePi launches n goroutines to compute an
// series-approximation of pi.
func CalculatePi(n int) float64 {
ch := make(chan float64)
for k := 0; k <= n; k++ {
// calculate k-th term in the series
go term(ch, float64(k))
}
f := 0.0
//wait for all goroutines to complete, get and sum up their results:
for k := 0; k <= n; k++ {
f += <-ch
}
return f
}
func term(ch chan float64, k float64) {
ch <- 4 * math.Pow(-1, k) / (2*k + 1)
}
/* Output:
3.14179261359579
The calculation took this amount of time: 0.028002
*/