<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'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 & 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'll test and if it beats the current implementation I'll merge and now you're the one to beat!</p>
<hr/>**评论:**<br/><br/>djherbis: <pre><p>The strings you're benchmarking on seem too small imho to actually see real perf. differences.</p>
<p>I think your "large" 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'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's pretty easy to change this to have 'unique middle' 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'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'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 & 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'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'd love to take a look and see what'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'm afraid I don't understand your comment? but if your suggesting there's a better way, please let me know :)</p></pre>tv64738: <pre><p>In which case you probably shouldn't be aiming to create "the most efficient" 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'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're trying to create work. For example, understanding <em>why</em> that one line is unlikely to show up in any "most efficient" anything.</p></pre>PaulCapestany: <pre><p>It's too early in the morning, so I might be missing something obvious, but what'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传