[noob] python vs golang vs golang with chan.

xuanbao · · 509 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Can anyone give me a short(ideally less than 40 lines) benchmark example of a python vs golang no channel vs golang + channel showing golang + channel &gt; golang no channel &gt; python in terms of speed? </p> <p>I have been trying to understand the benchmarks in <a href="http://benchmarksgame.alioth.debian.org/" rel="nofollow">here</a>, however they are too complicate for me to understand. </p> <hr/>**评论:**<br/><br/>Sythe2o0: <pre><p>Well, it depends on the benchmark-- in those comparisons you can see that Go doesn&#39;t out perform python on the regex-redux benchmark. </p> <p>Furthermore, the use of channels doesn&#39;t make Go faster. If you are comparing a synchonous application and Go with channels to enable parallelism (not just concurrency), then the performance improvement will depend on the number of threads the computer can execute in parallel. </p> <p>Yet still, it&#39;ll be faster to use raw mutexes over channels in a parallel system. Channels are not a tool for high performance applications.</p></pre>nevyn: <pre><blockquote> <p>Yet still, it&#39;ll be faster to use raw mutexes over channels in a parallel system. Channels are not a tool for high performance applications.</p> </blockquote> <p>This is a bad way to think of it IMO. Yes, if you optimize all the way down you are going to be using specific locks or even lock-free/atomic magic. BUT Channels are a great way to get the easy wins.</p> <p>Eg. You have a producer doing stuff and yield&#39;ing results to a for loop in python, threading this will suck ... but even a single (go func() -&gt; channel -&gt; range) change is likely to be a significant win, and it&#39;s much easiest to have N producers or consumers.</p></pre>juniorsysadmin1: <pre><p>I just want a real world example a simple one that i could understand on why go channel is awesome. The channel is the hype about go when people talk about it. </p></pre>Sythe2o0: <pre><p>I like the <a href="https://golang.org/doc/play/sieve.go" rel="nofollow">concurrent prime sieve</a> example.</p></pre>jerf: <pre><p>Well, to some extent, there isn&#39;t much more to understand. Go is generally faster than Python. However it is certainly possible to write a program in Go to solve a problem that would be slower than one in Python, for all the reasons any two programs can be slower. What the benchmarks really say is that if you try to optimize, you can generally go farther in Go.</p> <p>(One thing I&#39;ll warn you about is that channels, while something a Python programmer would find pretty fast, are nowhere near free. I&#39;ve lost track of the number of people who post on <a href="/r/golang" rel="nofollow">/r/golang</a> &#34;I wrote a program to add the first billion integers, and then I wrote a program that spawned a goroutine for each addition which it received via a channel and why is that hundreds of times slower?&#34; Well, you see, on a modern processor an integer addition can take <em>less</em> than one cycle (amortized), whereas starting goroutines and using a channel can each be dozens or hundreds of instructions and maybe a few cache misses (depending), so, yeah, that&#39;s a lot slower when you do that for each addition. Goroutines and channels do not magically parallelize things for free. I mention this because I see it a lot.)</p></pre>nevyn: <pre><blockquote> <p>One thing I&#39;ll warn you about is that channels, while something a Python programmer would find pretty fast, are nowhere near free</p> </blockquote> <p>But nothing is, and it&#39;s only the very beginner who thinks you need/want to parallelize obviously very serial operations. On the other hand let me introduce you to my old &#34;friend&#34; import multiprocessing.</p></pre>jerf: <pre><blockquote> <p>But nothing is, and it&#39;s only the very beginner who thinks you need/want to parallelize obviously very serial operations.</p> </blockquote> <p>I&#39;m not just making this up. I&#39;ve seen this exact issue at least three times here. And I&#39;m being conservative in that estimate. (I&#39;ve also seen the equivalent on <a href="/r/haskell" rel="nofollow">/r/haskell</a> at least twice, and I&#39;m sure the only reason I&#39;ve never seen it on <a href="/r/erlang" rel="nofollow">/r/erlang</a> is that I don&#39;t visit there.)</p> <blockquote> <p>On the other hand let me introduce you to my old &#34;friend&#34; import multiprocessing.</p> </blockquote> <p>Well, I&#39;m sure that spawning an entire process, even with fork, with a Python runtime on the other end will be even slower to add two numbers together with. I&#39;m not sure what your point is. Nobody&#39;s claiming Python has no solution at all for &#34;multiprocessing&#34;, but nobody sane is claiming it&#39;s going to perform anywhere near as well as Go.</p> <p>Python is ultimately a single-threaded language, born in an era where that was the norm, and it&#39;s got a collection of hacks of varying quality to try to get around that, but there&#39;s just no retrofitting it on to an implementation that was already ten years old when they <em>started</em> to think about these issues. That&#39;s not a Python-specific criticism; I&#39;ve <em>never</em> seen a 10-year-old implementation of any language grow any truly performant and usable concurrency support without the full support of a commercial enterprise behind it, and the ability to take multiple versions to shake out. Python is in fact doing the best of all the ones I&#39;ve seen try, so I&#39;m not being critical of Python specifically.</p></pre>drvd: <pre><p>May I ask why everybody is so fixed on speed? Is this the only thing to consider when choosing a technology? And if yes: Shouldn&#39;t the measurement of &#34;speed&#34; be a realistic one (and thus the benchmark probably complicated)? </p></pre>kaeshiwaza: <pre><p>Speed to maintain an old app matter ! (and I bet Go will win)</p></pre>nasciiboy: <pre><p>python is a language for prompts and extensions, if you use it for something else, you make a mistake</p></pre>ChristophBerger: <pre><p>Tell this the Web development, scientific, machine learning, or AI communities and watch their responses closely. (But I guess you were only joking.)</p></pre>kostix: <pre><p>There&#39;s such thing as &#34;established practices&#34;.</p> <p>At different points of time of the human history people were absolutely sure a horse is the only way to move a cart and ships made of steel would definitely sink.</p> <p>These days you could see those established practices had little to do with the technologies which disrupted them, eventually :-P</p></pre>

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

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