<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 "github.com/tuxychandru/golang-pkg-pcre/src/pkg/pcre" which is just a wrapper of libpcre. It'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&lang=go&id=1" rel="nofollow">https://benchmarksgame.alioth.debian.org/u64q/program.php?test=regexredux&lang=go&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&message=Excludeme&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>"classic C regex" <a href="https://benchmarksgame.alioth.debian.org/u64q/program.php?test=regexredux&lang=go&id=2" rel="nofollow">Go #2 program</a></p></li>
<li><p>"standard library regex" <a href="https://benchmarksgame.alioth.debian.org/u64q/program.php?test=regexredux&lang=go&id=1" rel="nofollow">Go program</a></p></li>
</ul>
<p>Maybe it's the way the programs are written.</p></pre>shovelpost: <pre><p>What's your point?</p></pre>igouy: <pre><p>The faster <a href="https://benchmarksgame.alioth.debian.org/u64q/program.php?test=regexredux&lang=go&id=2" rel="nofollow">regex-redux Go #2 program</a> is not -- <em>"different than the classic C regex that most other languages use".</em></p></pre>shovelpost: <pre><blockquote>
<p>The faster regex-redux Go #2 program is not -- "different than the classic C regex that most other languages use".</p>
</blockquote>
<p>Well, obviously! That's what the very first comment said: </p>
<blockquote>
<p>The regexredux test uses "github.com/tuxychandru/golang-pkg-pcre/src/pkg/pcre" which is just a wrapper of libpcre. It'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's regex (stdlib) will usually be slower but that'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'll have the knowledge on how to investigate this and let us know why Go is so slow. And you'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'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't have "that first line on main" <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't understand why ppl downvoted this comment.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传