日常代码记录 golang用协诚进行计算以及乱七八糟的代码
程序员文章站
2022-05-20 20:25:54
...
goalng代码
func add(i,len int, ch chan int) {
count := 0
for i:=i; i<len; i++{
count = count + i
}
ch <- count
}
func (u *UserController) Index() {
t1 := time.Now()
fmt.Printf("开始时间:%s,\n",t1)
NCPU := runtime.NumCPU()
gorun := 20
runtime.GOMAXPROCS(NCPU)
chs := make([] chan int,gorun)
for i := 0; i<gorun; i++ {
chs[i] = make(chan int)
n := 20 /gorun
go add(i*n,n*(i+1),chs[i])
}
count := 0
for i2:=0; i2<gorun ; i2++ {
ch := <- chs[i2]
count = count + ch
}
uid,err:= u.GetInt("uid")
if err != nil {
fmt.Print("参数错误\n")
}
userInfo := models.NewUser().UserAll(uid)
josnUser,err := json.Marshal(userInfo)
//fmt.Print(userInfo)
fmt.Print(userInfo)
fmt.Print(josnUser)
//fmt.Printf("接受到uid为:%d,\n",uid)
//fmt.Printf("cpu num:%d,count:%d\n",NCPU,count)
//fmt.Print("我是一个顽强的小强\n")
//admin := u.Get()
u.Data["Website"] = count
u.Data["Email"] = "[email protected]"
u.TplName = "index.tpl"
}
上一篇: 深度优先搜索算法