在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 package main 2 3 import "fmt" 4 5 type Vector []float64 6 7 func (v Vector) DoSome(i,n int, u Vector, c chan float64) { 8 var sum float64 9 for ; i<n; i++ { 10 sum += u[i] 11 } 12 c <- sum 13 } 14 15 const NCPU = 2 16 17 func (v *Vector) DoAll(u Vector) { 18 c := make(chan float64, NCPU) //根据自己电脑的CPU产生对应个数的管道 19 20 for i:=0; i<NCPU; i++ { 21 go v.DoSome(i*len(u)/NCPU, (i+1)*len(u)/NCPU, u, c) 22 } 23 24 var sum float64 = 0.00 25 for i:=0; i<NCPU; i++ { 26 sum += <-c 27 } 28 fmt.Println(sum) 29 } 30 31 func main() { 32 var v Vector 33 u := []float64{1.00, 2.00, 3.00, 4.00, 5.00, 6.00} 34 35 v.DoAll(u) 36 }
|
请发表评论