老哥们,我把代码中的 runtime.GOMAXPROCS(1) 注释掉了,可是程序运行下来的时间根本没减少,这是为什么?
真是百思不得骑呐
package main
import (
"fmt"
"time"
"runtime"
)
var quit chan int = make(chan int)
func loop() {
for i := 0; i < 10000; i++ {
fmt.Printf("%d\n ", i)
}
quit <- 0
}
func main() {
fmt.Println(runtime.NumCPU())
time.Sleep(time.Second)
a := 500
t1 := time.Now()
runtime.GOMAXPROCS(1) //单核跑和把这句话注释吊(使用默认 CPU 个数)跑下来时间没差,这是为什么?
for i := 1; i <= a; i++ {
go loop()
}
for i := 0; i < a; i++ {
<-quit
}
elapsed := time.Since(t1)
fmt.Println("运行时间:", elapsed)
}
// 下面是 GOMAXPROCS 的说明
// GOMAXPROCS sets the maximum number of CPUs that can be executing
// simultaneously and returns the previous setting. If n < 1, it does not
// change the current setting.
// The number of logical CPUs on the local machine can be queried with NumCPU.
// This call will go away when the scheduler improves.