一个goroutine的最小工作大小

huangapple go评论83阅读模式
英文:

minimum work size of a goroutine

问题

有人知道为了使goroutine有益,大致需要多少最小工作量(假设有空闲核心可用于卸载工作)吗?

英文:

Does anyone know approximately what the minimum work size is needed in order for a goroutine to be beneficial (assuming that there are free cores for the work to be offloaded to)?

答案1

得分: 3

我一直在使用Go语言解决项目欧拉问题。虽然我没有确切的答案,但我发现Go文档中基于goroutine的素数筛比简单地检查每个数字是否为素数要慢一个数量级。将GOMAXPROCS设置为更高的值也没有帮助。

英文:

I've been plodding through project euler with Go. While I don't have a definite answer for you I found the goroutine-based primality sieve in the Go docs to be an order of magnitude slower than simply checking each number for primality. Setting GOMAXPROCS to a higher value didn't help, either.

答案2

得分: 3

goroutine是一个抽象概念,如果它有助于更好地建模你的应用程序,你可以使用它。你正在进行并发编程,所以要考虑应用程序中具有并发性的部分。

想象一下面向对象的系统,并想象一下关于是否应该实例化对象的同样问题。

首先做有意义的事情。

英文:

goroutine is an abstraction that you use if it helps you model your application better. You're doing concurrency oriented programming, so think about the parts of your application that have concurrency within them.

Think about an OO system and imagine asking the same question about whether you should instantiate an object.

Do the thing that makes sense first.

答案3

得分: 1

使用goroutines不仅仅是为了提高硬件效率。有时它们使得软件更容易编写,并且更容易避免错误。这种语言允许程序员自然而简单地表达并发。对我来说,这非常有价值。

我个人在处理适合并发的问题时的经验是,使用go语言可以轻松地通过一个简单的“分散/聚集”方法将所有可用的CPU核心的利用率最大化。具体情况可能有所不同。

Hotei

英文:

Using goroutines isn't just about hardware efficiency. Sometimes they make the software easier to write and make it easier to keep bugs out. The language allows the programmer to express concurrency naturally and simply. That's worth a lot to me.

My own experience with problems that are natural candidates for concurrency is that go easily allows me to max out all the available cores on CPU bound problems using a trivial "scatter/gather" approach. Your mileage may vary.

Hotei

答案4

得分: 0

goroutines是轻量级的,不占用太多资源。在适合的问题上应该使用它们。目前,Go语言似乎在使用多核方面并不特别出色(似乎在分配额外的主机线程时有些开销过大)。

我认为真正的问题是何时使用多核,而不是何时使用goroutines。对于其他语言和额外的主机进程,答案可能是相同的。(不幸的是,你不能轻松地指定一个goroutine应该占用一个新的主机进程,或者它应该占用哪个进程。)

英文:

goroutines are lightweight and don't take up much resources. You should use them where ever it is appropriate to the problem. Currently go doesn't seem to be exceptionally good at using multiple cores (it seems there is a bit too much overhead in allocating additional host threads.)

I think the real question is when to use multiple cores rather than when to use goroutines. The answer to that is probably the same as for other languages and additional host processes. (Unfortunately you can't easily specify when a goroutine should occupy a new host process or which process it should occupy.)

huangapple
  • 本文由 发表于 2009年11月14日 11:59:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/1733205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定