invite/challenge for fun & profit

agolangf · · 492 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>In my a efforts to learn Rust I decided to create a strutil package containing common string functions and compare them to Go; this is not a Go vs Rust comparison, I merely decided to take a language I know well and compare it to it&#39;s Rust counterpart code. <a href="https://github.com/rust-playground/strutil" rel="nofollow">https://github.com/rust-playground/strutil</a> There are only a couple of functions currently but am looking to add more as I go.</p> <p>I am attempting to create the most efficient and idiomatic Go &amp; Rust code possible and would like to invite/challenge the community to contribute in making the best code possible!</p> <p>Hopefully everyone can learn something new :)</p> <p>If you can make any of the functions more efficient, with better throughput or less allocations, ... make a pull request, I&#39;ll test and if it beats the current implementation I&#39;ll merge and now you&#39;re the one to beat!</p> <hr/>**评论:**<br/><br/>djherbis: <pre><p>The strings you&#39;re benchmarking on seem too small imho to actually see real perf. differences.</p> <p>I think your &#34;large&#34; examples should contain much more data to get a better benchmark, otherwise noise will likely triumph.</p></pre>dean_karn: <pre><p>Thanks @djherbis how long would you suggest? Or if you have an example I&#39;d definitely add it :)</p></pre>djherbis: <pre><p>For happy paths (returns true):</p> <p>For palindromes, generate some long string of random runes and then just reverse them and add forward + backward string to get a generated palindrome (it&#39;s pretty easy to change this to have &#39;unique middle&#39; char if you want).</p> <p>For anagrams, generate a long string of random runes, and then use <a href="https://golang.org/pkg/math/rand/#Rand.Perm" rel="nofollow">rand.Perm</a> to get a random permutation of them (which will be an anagram).</p> <p>(you should probably save the generated strings for re-use so your tests are repeatable)</p> <p>This lets you generate arbitrarily big test data, I&#39;d probably start with ~1000 runes and compare a few implementations to see what kind of perf. difference I see after running it a number of times. I&#39;d fiddle with the size of the generated test cases (* or / by powers of 2) until I saw a significant difference in perf.</p> <p>Some implementations might optimize for different cases too (like expecting unhappy vs. happy path vs. balanced).</p> <p>I think these kinds of challenges are neat, have fun!</p></pre>dean_karn: <pre><p>Thanks, I will implement immediately..done</p></pre>tv64738: <pre><blockquote> <p>I am attempting to create the most efficient and idiomatic Go &amp; Rust code possible</p> </blockquote> <p>5 seconds in:</p> <p><a href="https://github.com/go-experimental/strutil/blob/18f1abbe29f10d870520c262784321c8233e840e/strutil.go#L11" rel="nofollow">https://github.com/go-experimental/strutil/blob/18f1abbe29f10d870520c262784321c8233e840e/strutil.go#L11</a></p></pre>ar1819: <pre><p>This will not work when two or more runes produces one symbol.</p> <p>Unicode is hard... </p></pre>karnd01: <pre><p>You are correct @ar1819 these functions only support single unicode runes symbols not multiple rune symbols, I&#39;ll add that to the documentation of these functions, thanks :)</p> <p>As a side note could you provide an example of a multi-columns symbol? I&#39;d love to take a look and see what&#39;s possible.</p></pre>ar1819: <pre><p>Here you go: <a href="https://play.golang.org/p/CVXNYEVFmA" rel="nofollow">https://play.golang.org/p/CVXNYEVFmA</a></p></pre>Robonia: <pre><p>So would normalizing the runes fix this issue?</p></pre>ar1819: <pre><p>No - the results would become invalid: <a href="https://play.golang.org/p/klXld3DSBu" rel="nofollow">https://play.golang.org/p/klXld3DSBu</a></p></pre>dean_karn: <pre><p>I&#39;m afraid I don&#39;t understand your comment? but if your suggesting there&#39;s a better way, please let me know :)</p></pre>tv64738: <pre><p>In which case you probably shouldn&#39;t be aiming to create &#34;the most efficient&#34; anything, at this time. Sorry.</p></pre>dean_karn: <pre><p>@tv64738 if you have nothing positive to contribute, why are you even commenting here?</p> <p>I hear a lot of negativity and no suggestions for improvement, which is what this whole endeavour is about...so I will assume you have none :)</p></pre>tv64738: <pre><p>Because you&#39;re misleading people into thinking something there is efficient, idiomatic, and a good comparison of two things.</p> <p>An improvement would be reading about how the things you&#39;re trying to create work. For example, understanding <em>why</em> that one line is unlikely to show up in any &#34;most efficient&#34; anything.</p></pre>PaulCapestany: <pre><p>It&#39;s too early in the morning, so I might be missing something obvious, but what&#39;s inefficient about casting a string to a slice of runes?</p></pre>ar1819: <pre><p>Because of the additional allocation, with copying perhaps? There is no way to get reverse iterator in Go, and indexing string returns byte and not rune. Still, this is not a reliable way to tell if the string is a palindrome. See comment below about that.</p></pre>

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

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