Testing performance: c vs goLang with a simple quicksort. I'm perplexed by my results, please help me understand.

polaris · · 797 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I was interested in testing the relative performance of c vs goLang so I undertook a <em>VERY</em> unscientific test.</p> <p>I found a simple quicksort algorithm on github and implemented it in c and golang, copying the code almost line for line in both languages. I then sorted a random array of 100MM ints.</p> <p>Both executables consumed 100% of my cpu, and surprisingly, goLang consistently performs the sort in half the time! It doesn&#39;t matter the array size, goLang is always faster.</p> <p>This has me a bit confused. I was under the impression that C was supposed to be incredibly fast. What am I misunderstanding? Thanks.</p> <p><a href="https://github.com/adam-hanna/c-vs-goLang-Quicksort">Here&#39;s the code if you&#39;d like to take a look.</a></p> <hr/>**评论:**<br/><br/>barsonme: <pre><p>What C compiler + flags did you use? And what Go version?</p> <p>edit:</p> <p>So, using GCC (5.2.0) <code>-O0</code> runs at around 40 seconds. <code>-O3</code> runs at ~17 seconds.</p> <pre><code>eric@archbox /tmp/c-vs-goLang-Quicksort/c $ ls Makefile quicksort.c quicksort.h eric@archbox /tmp/c-vs-goLang-Quicksort/c $ gcc quicksort.c &amp;&amp; time ./a.out 0: 1804289383, 1: 846930886, 2: 1681692777 took 38491.886719 0: 7, 1: 37, 2: 51 real 0m39.748s user 0m39.650s sys 0m0.063s eric@archbox /tmp/c-vs-goLang-Quicksort/c $ gcc quicksort.c -O3 &amp;&amp; time ./a.out 0: 1804289383, 1: 846930886, 2: 1681692777 took 16032.280273 0: 7, 1: 37, 2: 51 real 0m17.208s user 0m17.130s sys 0m0.063s eric@archbox /tmp/c-vs-goLang-Quicksort/c $ </code></pre> <p>Go 1.5 runs at ~22.5 seconds.</p> <pre><code>eric@archbox /tmp/c-vs-goLang-Quicksort/goLang $ ls quicksort.go eric@archbox /tmp/c-vs-goLang-Quicksort/goLang $ go build &amp;&amp; time ./goLang a1[0]: 5577006791947779410, a1[1]: 8674665223082153551, a1[2]: 6129484611666145821 n of 100000000 took 18.312330137s to run. a1[0]: 30554741346, a1[1]: 217339476874, a1[2]: 223812424683 real 0m22.488s user 0m22.353s sys 0m0.193s eric@archbox /tmp/c-vs-goLang-Quicksort/goLang $ </code></pre> <p>Go always builds with optimizations on. Rob&#39;s said before in go-nuts that the reason there&#39;s no compiler optimization options if there&#39;s any they can use, they&#39;ll (safely) use them. With most C compilers, you have to turn on optimizations.</p> <p>The 17-22.5 second difference could very well be GC + Go&#39;s immaturity.</p></pre>bonekeeper: <pre><p>21seconds against C&#39;s 17 seconds with -O3 is still surprisingly fast IMO - I wonder how Java would perform here, since Java was still outperforming Go in a bunch of synthetic tests from the language shootout not long ago.</p> <p>EDIT: fixed the figures</p></pre>barsonme: <pre><blockquote> <p>17 seconds against C&#39;s 21 seconds with -O3 </p> </blockquote> <p>22.5 seconds vs C&#39;s 17 seconds, but yeah.</p> <p>I think it could easily match Go if you ignore warmup time. I tried writing this in Java but I&#39;ve actually never written Java before and I keep getting heap overflow errors, probably due to using <code>Arrays.copyOfRange()</code> recursively.</p></pre>Telefonica46: <pre><blockquote> <p>I keep getting heap overflow errors</p> </blockquote> <p>I got the same thing in c when I was trying to initialize the 100MM array in main(). My understanding is either it needs to be created outside of main as a global var, or inside of main using malloc(). Maybe the same is true in java.</p> <p>EDIT: From your comment below, it looks like you got it working!</p></pre>barsonme: <pre><p>Yeah, the Java method I was using was creating a physical copy of the array. The C version uses pointers to one single array and the Go version uses slices which contain a pointer to an array.</p> <p>Java&#39;s weird

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

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