-
Notifications
You must be signed in to change notification settings - Fork 0
/
035.Goroutine_CalcPi.go
79 lines (65 loc) · 1.79 KB
/
035.Goroutine_CalcPi.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"math"
"runtime"
"sync"
"time"
)
func PrintNumGoroutine() {
fmt.Println("NumGoroutine:", runtime.NumGoroutine())
}
func main() {
var termsNum float64 = 1E11
var startTime, endTime time.Time
startTime = time.Now()
CalcPiByGoroutine(termsNum)
endTime = time.Now()
fmt.Println("CalcPiByGoroutine() Spent time:", endTime.Sub(startTime))
startTime = time.Now()
CalcPi(termsNum)
endTime = time.Now()
fmt.Println("CalcPi() Spent time:", endTime.Sub(startTime))
}
func CalcPiByGoroutine(numOfTrem float64) {
fmt.Println("runtime.NumCPU:", runtime.NumCPU())
var SqrtPiDivieBy6 float64
partSumStream := make(chan float64, runtime.NumCPU())
wg := &sync.WaitGroup{}
//每一個核心分配到的加總工作
var GetPartSum = func(start float64, step float64, upper float64, sumStream chan<- float64, wg *sync.WaitGroup) {
var thePartSum float64
defer wg.Done() // return 前保證執行
fmt.Println(start, step, upper)
for i := start; i <= upper; i += step {
thePartSum += 1.0 / (i * i)
// fmt.Printf("start=%d, i=%f\n", start, i)
}
sumStream <- thePartSum
}
for coreNum := 1; coreNum <= runtime.NumCPU(); coreNum++ {
wg.Add(1)
go GetPartSum(float64(coreNum), float64(runtime.NumCPU()), numOfTrem-float64(runtime.NumCPU()-coreNum), partSumStream, wg)
}
PrintNumGoroutine()
wg.Wait()
close(partSumStream)
SqrtPiDivieBy6 = func() (sum float64) {
for part := range partSumStream {
fmt.Println("part:", part)
sum += part
}
return sum
}()
var pi = math.Pow(SqrtPiDivieBy6*6, 0.5)
fmt.Printf("Pi=%f\n", pi)
}
func CalcPi(numOfTrem float64) {
var SqrtPiDivieBy6 float64
for i := 1.0; i <= numOfTrem; i++ {
SqrtPiDivieBy6 += 1.0 / (i * i)
}
PrintNumGoroutine()
var pi = math.Pow(SqrtPiDivieBy6*6, 0.5)
fmt.Printf("Pi=%f\n", pi)
}