Why is Go so slow?

agolangf · · 605 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Go is not always fast. Here is the performance of the regular expressions: <a href="https://benchmarksgame.alioth.debian.org/u64q/regexredux.html" rel="nofollow">https://benchmarksgame.alioth.debian.org/u64q/regexredux.html</a> Another example, binary trees: <a href="https://benchmarksgame.alioth.debian.org/u64q/binarytrees.html" rel="nofollow">https://benchmarksgame.alioth.debian.org/u64q/binarytrees.html</a></p> <hr/>**评论:**<br/><br/>reus: <pre><p>The regexredux test uses &#34;github.com/tuxychandru/golang-pkg-pcre/src/pkg/pcre&#34; which is just a wrapper of libpcre. It&#39;s nonsense to benchmark Go program that calling C library functions through Cgo. Cgo is not go.</p></pre>danilobuerger: <pre><p>This one is only stdlib and seems to be even slower: <a href="https://benchmarksgame.alioth.debian.org/u64q/program.php?test=regexredux&amp;lang=go&amp;id=1" rel="nofollow">https://benchmarksgame.alioth.debian.org/u64q/program.php?test=regexredux&amp;lang=go&amp;id=1</a></p></pre>shovelpost: <pre><p>The standard library regex is different than the classic C regex that most other languages use. So: </p> <ul> <li><p>In this case, Go is not competing against Ruby, Python etc. It is competing against C.</p></li> <li><p>Most importantly, the algorithm Go uses guarantees to complete in time that is linear in the length of the input. Meanwhile the classic C algorithm that is used by Python, Ruby etc. can take time exponential to the length of the input, which in practice means that you are vulnerable to <a href="https://en.wikipedia.org/wiki/ReDoS" rel="nofollow">ReDoS</a> attacks.</p></li> </ul></pre>WikiTextBot: <pre><p><strong>ReDoS</strong></p> <p>The regular expression denial of service (ReDoS) is an algorithmic complexity attack that produces a denial-of-service by providing a regular expression that takes a very long time to evaluate. The attack exploits the fact that most regular expression implementations have exponential time worst case complexity: the time taken can grow exponentially in relation to input size. An attacker can thus cause a program to spend an unbounded amount of time processing by providing such a regular expression, either slowing down or becoming unresponsive.</p> <hr/> <p><sup>[</sup> <a href="https://www.reddit.com/message/compose?to=kittens_from_space" rel="nofollow"><sup>PM</sup></a> <sup>|</sup> <a href="https://reddit.com/message/compose?to=WikiTextBot&amp;message=Excludeme&amp;subject=Excludeme" rel="nofollow"><sup>Exclude</sup> <sup>me</sup></a> <sup>|</sup> <a href="https://np.reddit.com/r/golang/about/banned" rel="nofollow"><sup>Exclude</sup> <sup>from</sup> <sup>subreddit</sup></a> <sup>|</sup> <a href="https://np.reddit.com/r/WikiTextBot/wiki/index" rel="nofollow"><sup>FAQ</sup> <sup>/</sup> <sup>Information</sup></a> <sup>|</sup> <a href="https://github.com/kittenswolf/WikiTextBot" rel="nofollow"><sup>Source</sup></a> <sup>|</sup> <a href="https://www.reddit.com/r/WikiTextBot/wiki/donate" rel="nofollow"><sup>Donate</sup></a> <sup>]</sup> <sup>Downvote</sup> <sup>to</sup> <sup>remove</sup> <sup>|</sup> <sup>v0.28</sup></p></pre>igouy: <pre><p>Both:</p> <ul> <li><p>&#34;classic C regex&#34; <a href="https://benchmarksgame.alioth.debian.org/u64q/program.php?test=regexredux&amp;lang=go&amp;id=2" rel="nofollow">Go #2 program</a></p></li> <li><p>&#34;standard library regex&#34; <a href="https://benchmarksgame.alioth.debian.org/u64q/program.php?test=regexredux&amp;lang=go&amp;id=1" rel="nofollow">Go program</a></p></li> </ul> <p>Maybe it&#39;s the way the programs are written.</p></pre>shovelpost: <pre><p>What&#39;s your point?</p></pre>igouy: <pre><p>The faster <a href="https://benchmarksgame.alioth.debian.org/u64q/program.php?test=regexredux&amp;lang=go&amp;id=2" rel="nofollow">regex-redux Go #2 program</a> is not -- <em>&#34;different than the classic C regex that most other languages use&#34;.</em></p></pre>shovelpost: <pre><blockquote> <p>The faster regex-redux Go #2 program is not -- &#34;different than the classic C regex that most other languages use&#34;.</p> </blockquote> <p>Well, obviously! That&#39;s what the very first comment said: </p> <blockquote> <p>The regexredux test uses &#34;github.com/tuxychandru/golang-pkg-pcre/src/pkg/pcre&#34; which is just a wrapper of libpcre. It&#39;s nonsense to benchmark Go program that calling C library functions through Cgo. Cgo is not go.</p> </blockquote> <p>Then the person after that replied:</p> <blockquote> <p>This one is only stdlib and seems to be even slower</p> </blockquote> <p>So my whole point was that Go&#39;s regex (stdlib) will usually be slower but that&#39;s only because it offers other more practical advantages.</p></pre>dlsniper: <pre><blockquote> <p>Go is not always fast.</p> </blockquote> <p>That is a good assessment.</p> <p>As for why is it slow, you can read this <a href="https://blog.golang.org/profiling-go-programs" rel="nofollow">https://blog.golang.org/profiling-go-programs</a> and watch this <a href="https://youtu.be/ySy3sR1LFCQ" rel="nofollow">https://youtu.be/ySy3sR1LFCQ</a> and then you&#39;ll have the knowledge on how to investigate this and let us know why Go is so slow. And you&#39;ll also be able to improve them if possible so that the results are better.</p> <p>Looking forward for your analysis. Good luck.</p></pre>nagai: <pre><p>The bintree bench measures the cost of performing hundreds of millions of allocations, and go is a strictly garbage collected language so do the math.</p></pre>igouy: <pre><p>C#, Erlang, Java, Haskell, Racket, Lisp, Smalltalk, … also are GC implementations.</p></pre>dlsniper: <pre><p>So... Have you profiled it? Do you want to let us know why this is happening then? </p></pre>igouy: <pre><p>I don&#39;t need to have profiled the program to understand that the explanation provided by <code>nagai</code> is insufficient, given the other GC implementations.</p></pre>victrolla: <pre><p>Because they’re very poorly written benchmarks. </p></pre>dlsniper: <pre><p>How did you arrived to this conclusion? And how can they be improved in that case? </p></pre>shovelpost: <pre><p>Binary trees, first line on main:</p> <pre><code>runtime.GOMAXPROCS(runtime.NumCPU() * 2) </code></pre> <p>Your title:</p> <pre><code>Why is Go so slow? </code></pre> <p>Your first line:</p> <pre><code>Go is not always fast. </code></pre></pre>igouy: <pre><p>The other 5 Go binary-trees programs don&#39;t have &#34;that first line on main&#34; <a href="http://benchmarksgame.alioth.debian.org/u64q/measurements.php?lang=go" rel="nofollow">and are slower</a>.</p></pre>gbitten: <pre><p>I don&#39;t understand why ppl downvoted this comment.</p></pre>

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

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