dongsuo0517 2019-07-24 09:02
浏览 133

在runtime.GOMAXPROCS(1)时,为什么单个goroutine运行慢于多个goroutine?

I just want to try how fast goroutine switch context, so I wrote the code below. To my surprise, multiple gorountines run faster than the edition that does not need to switch context (I set the program to run in only one CPU core).

package main

import (
    "fmt"
    "runtime"
    "sync"
    "time"
)

func main() {
    runtime.GOMAXPROCS(1)
    t_start := time.Now()
    sum := 0
    for j := 0; j < 10; j++ {
        sum = 0
        for i := 0; i < 100000000; i++ {
            sum += i
        }
    }
    fmt.Println("single goroutine takes ", time.Since(t_start))

    var wg sync.WaitGroup
    t_start = time.Now()

    for j := 0; j < 10; j++ {
        wg.Add(1)
        go func() {
            sum := 0
            for i := 0; i < 100000000; i++ {
                sum += i
            }
            defer wg.Done()
        }()
    }
    wg.Wait()
    fmt.Println("multiple goroutines take ", time.Since(t_start))
}

A single goroutine takes 251.690788ms, multiple goroutines take 254.067156ms

The single goroutine should run faster, because single goroutine does not need to change context. However, the answer is opposite, single mode always slower. What happened in this program?

  • 写回答

1条回答 默认 最新

  • doute3621 2019-07-24 09:35
    关注

    Your concurrent version several things the non-concurrent version does, which will make it slower:

    1. It's creating a new sum value, which must be allocated. Your non-concurrent version just resets the existing value. This probably has a minimal impact, but is a difference.
    2. You're using a waitgroup. Obviously this adds overhead.
    3. The defer in defer wg.Done() also adds overhead, roughly equivalent to an extra function call.

    There may well be other subtle differences, too.

    So in short: Your benchmarks are just invalid, because you're comparing apples with oranges.

    More important: This isn't a useful benchmark in the first place, because it's a completely artificial workload.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效