douli1872 2018-03-18 11:00
浏览 18

在“ for {select}”结构中添加了一个简单的fmt.Println之后,CPU使用率完全不同,为什么?

Encounter a confused situation :
let's say we have a for { select } function which is written in Go.
Here the code below:

package main
//import "fmt"
func main(){

  for {
    select {

      default:
      _=1                     1. first situation 
      // fmt.Sprint("aa")     2. second situation 
          }
  }
}

while in the first situation the cup usage shows below: enter image description here in the second situation the cpu usage shows below: enter image description here

I guess something happened in fmt.Println .
May be related to the mechanism of Go's fmt realization?
Not quite sure how it happens by using all the CPUs?
Thanks in advance !

  • 写回答

1条回答 默认 最新

  • doujue9767 2018-03-19 01:57
    关注

    My guess: In the first statement go's scheduler can't work. Go's scheduler is semi cooperative, and only works at certain points. Like function calls for example.

    In an infinite loop with no function calls the scheduler has no chance to kick in..

    评论

报告相同问题?