Suggestion for performance improvement

agolangf · · 471 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><a href="https://github.com/kavehmz/prime">https://github.com/kavehmz/prime</a></p> <p>I wrote a prime number generator while ago when I was learning Go (Sieve method). Then I got time to improve it a bit (Segmented Sieve).</p> <p>Then I ran pprof and improved the performance as much as I could.</p> <p>Can anyone suggest any change that improves it even further? Not a new method but any Go improvement.</p> <hr/>**评论:**<br/><br/>dlsniper: <pre><p>I might also add: do not call GOMAXPROCS from a library as you will affect the main application running in ways the author has no control over.</p></pre>kavehmz: <pre><p>True, No need.</p></pre>abcded1234234: <pre><p>Have you tried to compare performance of a single threaded version to your parallel version. What are the results?</p> <p>Try experimenting with the segment size, if a single segment can fit into L1 cache the code will run faster.</p> <p>Some advice not related to performance:</p> <ul> <li> do not call <code>runtime.GOMAXPROCS(cores)</code> from your functions. This is no longer needed in Go 1.5+</li> <li>consider using sync.WaitGroup instead of channels</li> <li>why are you calling <code>next &lt;- true</code> NumCPU() times? This doesn&#39;t look right.</li> <li>build your program with <code>-race</code>flag and run it to make sure there are no race conditions</li> </ul></pre>kavehmz: <pre><p>next &lt;- true is for waiting for last goroutines to end. Sure, I will try sync and see if it helps. But I can&#39;t imagine how. Thanks, will check -race too.</p></pre>kavehmz: <pre><p>single threaded if you mean no go routine, I checked and it was slower.</p></pre>motojo: <pre><p>Using a single thread, this is what I wrote based off my intern&#39;s way of counting the number of prime numbers: <a href="https://gist.github.com/josephspurrier/c3073854dd718f5c8021" rel="nofollow">https://gist.github.com/josephspurrier/c3073854dd718f5c8021</a></p></pre>kavehmz: <pre><p>This is slower. I want to improve the performance.</p> <p>I also I want to return the numbers.</p></pre>kavehmz: <pre><p>Also you are doing it non-segmented way that will use much more memory.</p> <p><a href="https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" rel="nofollow">https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes</a></p> <p>Check the Segmented Sieve part.</p> <p>Anyway I am looking for Go suggestions/tricks.</p></pre>

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

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