a newbie question about go

blov · · 655 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>what I am reading is that Go&#39;s main feature is the ability to use multi-core CPUs, and it is so easy (comparing to other programming languages). Is that true? I am just debating the benefits of the time investments i will put in learning the language</p> <hr/>**评论:**<br/><br/>BraveNewCurrency: <pre><p>You do not need a specific language to take advantage of multiple cores. Even the single-threaded Node.js language has a simple way to use multiple cores: Just run multiple copies of your program, and have a tiny program hand out web requests to the different cores. As long as the &#34;work to be done&#34; is much longer than the &#34;work to hand out the work to be done&#34;, it can work. The &#34;handout code&#34; must be specially coded and worry about performance/memory/etc, but usually only needs to be written once.</p> <p>See <a href="https://en.wikipedia.org/wiki/Embarrassingly_parallel" rel="nofollow">Embarrassingly Parallel</a> problems.</p> <p>On the other hand, if you are doing a lot of very short jobs, or if the jobs need to talk to each other a lot, the &#34;work&#34; to hand them out will quickly become a bottleneck. In this case, Go is the perfect language. It&#39;s a good compromise between performance and simplicity. Other languages like C or Java can give performance, but debugging multi-threaded problems is a nightmare.</p> <p>This is a Go forum, so obviously, we&#39;re going to recommend Go. Personally, I liked the idea of Node.js, but it&#39;s &#34;callback style&#34; really turned me off -- you have to constantly jump around the page to follow the execution of the code, even in the simplest of examples.</p></pre>helinwang: <pre><p>You wrote other languages like c or java can give performance. I know its true for c to give more performance than go, is it true for java?</p></pre>BraveNewCurrency: <pre><blockquote> <p>I know its true for c to give more performance than go</p> </blockquote> <p>Statements like that are a bit misleading. There are many forces at work, and it&#39;s impossible to tease them all apart. But understanding them is the key to understanding performance. So let me try and explain:</p> <p>1) <strong>It&#39;s true that Go&#39;s compiler is fairly weak compared to very mature C compilers.</strong> So on average, Go code will be slower than C code. (This is improving dramatically over time). Go&#39;s garbage collection also takes CPU time. So first pass, Go will be slower than C for the &#34;same&#34; code.</p> <p>2) But all is not lost: <strong>For any program of sufficient complexity, a well-optimized program in a slow language will be much faster than the &#34;first pass&#34; of a program in a faster language.</strong> Programmers assume they know how to &#34;make things faster&#34;. But <a href="https://en.wikipedia.org/wiki/Amdahl%27s_law" rel="nofollow">speeding up random parts of your program won&#39;t make it faster</a>, which is why you hear &#34;optimization is the root of all evil&#34;. Only speed-ups in at the actual bottleneck tend to matter.</p> <p>In other words, better compilers can give you 2-5x performance, but changing your algorithm in one spot often gives you 10x-100x performance. Any experienced programmer can tell you war stories about this effect: Pulling some calculation out of a loop, unrolling a loop, memoization, caching, applying the <a href="https://en.wikipedia.org/wiki/Schwartzian_transform" rel="nofollow">Schwartzian transform</a>, etc.</p> <p>So the question is: Is Go a simpler language that leads to less time wasted debugging problems? (especially in a multi-threaded situation)? Does Go have better performance analysis tools to help you find the bottlenecks? If Go is faster to get working, can you take the time saved and apply it to optimizations? Would that optimized Go codebase perform better than an un-optimized C codebase? Would writing in Go make the programmer happy, and therefore not quit after the current project, thus saving money on maintenance next year? These are messy questions with no scientific way to answer them.</p> <p>There is also the <a href="https://en.wikipedia.org/wiki/Linguistic_relativity" rel="nofollow">Sapir–Whorf hypothesis</a> applied to computer languages: Let&#39;s say you want to write the word count program. If you start with Go, you will use a Hash because that&#39;s the &#34;natural&#34; data structure. If you start with C, you will likely start with an array, because a using a more complex data structure for performance is a lot more work. On a large data set, the better data structure will make the Go program faster to run. (Heck, the same argument applied to Perl, back in the day -- For some tasks, it would have taken weeks to write a C program that ran faster than a Perl program written in a day.)</p> <p>So some language features might accidentally lead you to better algorithms. And we know better algorithms are worth more than a &#34;slightly more optimized&#34; compiler.</p> <blockquote> <p>is it true for java?</p> </blockquote> <p>Yes, just like my Go example, there can be situations where Java accidentally leads you to a better algorithm, or gives you more time to do performance analysis when compared to C.</p> <p>For example, I pulled up a random <a href="https://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=gcc&amp;lang2=java" rel="nofollow">C vs Java</a> page on Debian&#39;s benchmarkgame. Sure enough, the last benchmark shows Java to be slightly faster. (And Java has a start-up time handicap, so it looks a lot worse on small data sets than large data sets.)</p></pre>heptara: <pre><p>To get the best answer, can you state what kind of applications you want to build and what language you are using at the moment?</p> <p>In general, concurrency is significantly less effort than most other languages. Go also an easy to learn language and it&#39;s safer than C. However, you&#39;ll get a better answer once the question above is answered. In programming, it&#39;s better to be explicit.</p></pre>gkar68: <pre><p>If Go main feature is the easy of using multi-core, which gives a boost in performance, so what I am targeting is building process-intensive applications. I am thinking of importing some open-source GIS libraries from c# into Go, and if I can see a boost in the performance. Is it worth that big investment?</p></pre>your_time_to_shine: <pre><p><a href="https://github.com/golang/go/wiki/Projects#gis" rel="nofollow">Here are some open-source GIS libraries in Go</a>.</p></pre>gkar68: <pre><p>wow thanks</p></pre>dhdfdh: <pre><p>Well, his only question is, &#34;Is using multi-core CPUs easy in Go?&#34;.</p></pre>malcor88: <pre><p>IMHO The go website has great examples on using concurrency / go routines without having to dedicating too much time into the language. </p></pre>bmurphy1976: <pre><p>Easy is relative. It&#39;s easier than dorking around with threads and locks, for sure, but it&#39;s still hard.</p> <p>That said, I would much rather have Go&#39;s flavor of CSP than just about every other concurrent framework I&#39;ve tried. Or Erlang. Well worth learning.</p></pre>

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

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