运行 同时存在数千个 goroutine

Running thousands of goroutines concurrently

我正在为历史交易数据开发模拟器模型。我有下面的伪代码,其中我使用了很多 goroutines。

start_day := "2015-01-01 09:15:00 "
end_day := "2015-02-01 09:15:00"
ch = make(chan float64)
var value bool

// pair_map have some int pairs like {1,2},{10,11} like approx 20k pairs which are taken
// dynamically from a range of input
for start_day.Before(end_day) {
    // a true value in ch is set at end of each func call
    go get_output(ch, start_day, pair_map)
    value <- ch
    start_day = start_date.Add(time.Hour * 24)
}

现在的问题是每个 get_otput() 大约执行一次。对于每天 20k 的组合(大约需要 1 分钟),我在这里尝试使用 go 例程(在单核机器上)进行一个月的时间跨度,但它花费的时间几乎与 运行 顺序(约 30 分钟)。

是我的做法有问题,还是因为我用的是单核?

I'm trying to do it for a month time span using go routines (on single core machine), but it is taking almost same amount of time as running sequentially

是什么让您认为它应该任何表现更好? 运行 多个 goroutines 有其开销:必须管理和调度 goroutines,必须传达/收集结果。如果你有一个核心,使用多个 gorotuines 不会产生性能提升,只会带来损害。

使用 goroutines 可能 如果你有多个内核并且你分配的任务足够“大”所以 goroutine 管理将小于并行的收益执行。

查看相关: