<p>I am trying to learn Go right now and I am looking for some hints from the pros on how to optimize code in terms of LOC as well as performance/efficiency.
An example of code I was working on here: <a href="http://play.golang.org/p/ehC53xA4OX">http://play.golang.org/p/ehC53xA4OX</a>
Which I think is generally already okay, though I would like to use
something like "type bigram [2]rune" instead of the []string.
For comparison I pulled up an old implementation of the algorithm I did a few years back in ruby: <a href="https://gist.github.com/anonymous/7fc2780832b5396d038d">https://gist.github.com/anonymous/7fc2780832b5396d038d</a> readability isn't great though</p>
<hr/>**评论:**<br/><br/>mkaz: <pre><p>LOC should not be what you optimize for; readability and comprehension are much better goals. Your future self with thank you.</p></pre>lwe: <pre><p>It's more of a challenge really. Same as writing write only Perl code </p></pre>dchapes: <pre><p>Exactly, my first thought when I saw the title was to say "smaller != better".</p></pre>lwe: <pre><p>On the other hand more verbose code is not automatically better <a href="https://medium.com/@webseanhickey/the-evolution-of-a-software-engineer-db854689243" rel="nofollow">https://medium.com/@webseanhickey/the-evolution-of-a-software-engineer-db854689243</a> </p></pre>dchapes: <pre><p>"smaller != better" is not at all the same thing as saying or implying that "verbose == better". There is very little relationship between terse/verbose and quality.</p></pre>a9y: <pre><p>A similar implementation of the same algorithm can be found <a href="https://groups.google.com/d/msg/golang-nuts/TTKPzAjwOV4/5315kJgCfWgJ">here</a>.</p>
<p>It doesn't perform as well as yours, but it does handle unicode and duplicates as one would expect.</p>
<p>The author chose to store the runes in a struct, however using a rune array for the <em>Bigram</em> type as you suggested would work just as well.</p>
<p>One quick suggestion about your implementation is that you should call <em>strings.ToLower</em> on the entire string, not on each slice. You get a noticeable performance hit calling it that many times.</p></pre>lwe: <pre><p>I found that one earlier. It's where I got the idea to use [2]rune. But I am still working out how to write that efficiently.</p>
<p>You're right about ToLower of course, I fixed that. Also found a bug in the first implementation. p2 = "" works on a copy not the actual array element. Had to change that.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传