我一直在用 Go 缓慢地完成 euler 项目。虽然我没有给你一个明确的答案,但我发现 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.
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.
我自己对并发自然候选问题的经验是,go 可以轻松地让我使用简单的“分散/聚集”方法来最大化 CPU 限制问题上的所有可用内核。您的里程可能会有所不同。
布袋
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.
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.)
发布评论
评论(4)
我一直在用 Go 缓慢地完成 euler 项目。虽然我没有给你一个明确的答案,但我发现 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.
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.
使用 goroutine 不仅仅是为了提高硬件效率。有时它们使软件更容易编写并且更容易排除错误。该语言允许程序员自然而简单地表达并发性。这对我来说很有价值。
我自己对并发自然候选问题的经验是,go 可以轻松地让我使用简单的“分散/聚集”方法来最大化 CPU 限制问题上的所有可用内核。您的里程可能会有所不同。
布袋
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
goroutine 是轻量级的,不会占用太多资源。您应该在适合解决问题的地方使用它们。目前,go 似乎不太擅长使用多核(分配额外的主机线程似乎有点太多的开销。)
我认为真正的问题是何时使用多核而不是何时使用 goroutine。答案可能与其他语言和附加主机进程相同。 (不幸的是,您无法轻松指定 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.)