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..

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法