How fast is doing a go func() for a block of code ?

blov · · 468 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Is there a performance hit from starting goroutines like this ? Is there a comparable Thread executor (in Java) i.e. goroutine executor ?</p> <hr/>**评论:**<br/><br/>tylermumford: <pre><blockquote> <p>Is there a performance hit from starting goroutines like this ?</p> </blockquote> <p>Just to be clear, this is the only way to start a goroutine. You probably know this, but your question could imply that there are more ways to start goroutines, which is false.</p></pre>hell_0n_wheel: <pre><p>The answer is always &#34;it depends&#34;, especially when you aren&#39;t telling us exactly how you&#39;re using the goroutine.</p> <p>Just your luck, there&#39;s a standard library that allows you to measure the performance yourself: <a href="https://golang.org/pkg/testing/" rel="nofollow">https://golang.org/pkg/testing/</a></p> <p>Wrap your code in a test, and run the test using <code>testing.B</code>. Tweak the code, re-run, bam, there&#39;s your answer.</p></pre>victrolla: <pre><p>There is a negligible performance hit but it would be highly dependent on what&#39;s going on. If you&#39;re asking about memory penalties, this is a non issue on modern systems. Goroutines do not have the memory overhead of the full runtime. </p> <p>If you&#39;re asking about CPU penalty, you need to test. It&#39;s not clear what your goroutines will be doing but on modern multi core machines, you likely won&#39;t notice anything but I encourage you to use the excellent profiler. Even if the goroutine blocks, you&#39;re not likely to realize it as long as there aren&#39;t severe resource contention. </p> <p>When you will notice is when you do things in goroutines like tight select loops. </p> <p>I&#39;m not familiar with java, but if the name hints to me what that is, you may want to look at the package sync.WaitGroup. </p> <p>Edit: side note, usually things slow down when you start using a mutex to coordinate things between goroutines. As a design principal you likely want to favor sending the data across a channel to avoid extraneous locks. </p></pre>titpetric: <pre><p>You can read <a href="https://rakyll.org/scheduler/" rel="nofollow">this article for more details on the Go scheduler</a>. The scheduler is basically similar to what a thread scheduler is in the OS kernel - it schedules work between goroutines. The performance hit is smaller than starting actual threads. I wouldn&#39;t know about a comparable thread executor, but the concept of &#39;coroutines&#39; is well known in other programming languages as well. Perhaps this is a question for <a href="/r/java" rel="nofollow">/r/java</a> more than here.</p> <p>Edit: <a href="https://stackoverflow.com/questions/2846664/implementing-coroutines-in-java" rel="nofollow">this stackoverflow post might provide some info into the state of goroutines in Java</a>.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

468 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传