Goroutine 的最小工作大小

发布于 2024-08-11 00:11:28 字数 1431 浏览 7 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福还没到 2024-08-18 00:11:28

我一直在用 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.

源来凯始玺欢你 2024-08-18 00:11:28

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.

心清如水 2024-08-18 00:11:28

使用 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

痞味浪人 2024-08-18 00:11:28

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文