Should slices have a second element access method?

blov · · 632 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m not very experienced in Goalng.</p> <p>In one my project, current, to avoid out_of_index runtime error, get_slice_element_1 is used. To access a valid element, there are 4 comparisons need. &#34;if index &lt; 0 || index &gt;= len (s)&#34; is also called in s [index].</p> <p>func get_slice_element_1 (s []int, index int) int {</p> <pre><code> if index &lt; 0 || index &gt;= len (s) { return 0 } return s [index] </code></pre> <p>}</p> <p>If we can add a default value parameter for invalid indexes then there will be only two comparisons.</p> <p>func get_slice_element_2 (s []int, index int) int {</p> <pre><code> return s.get_element (index, 0) </code></pre> <p>}</p> <hr/> <p>[edit] I suddenly realized adding a new parameter is not a good idea, for the cost of adding a new parameter may be larger than 2 comparisons. So I think this is a problem hardly to resolve for gc languages.</p> <hr/>**评论:**<br/><br/>natefinch: <pre><p>My suggestion is to skip the index &lt; 0 check. That&#39;s hiding programmer errors. If you ever try to get index -1, you&#39;re doing something wrong. You should look at why you&#39;d ever have a negative index. </p> <p>Also, built-ins aren&#39;t magic. They&#39;d do the same thing you&#39;re doing. </p></pre>TapirLiu: <pre><blockquote> <p>built-ins aren&#39;t magic. They&#39;d do the same thing you&#39;re doing.</p> </blockquote> <p>just for this reason, so I asked this question. They do it, and I do it again, it is a waste. I think it is a good idea only they do it, or only I do it.</p></pre>klauspost: <pre><p>Definitely not - you will see why when you&#39;ve done more Go. Do you have any <em>real world</em> (ie. not a for loop) application where that actually makes a measurable difference? </p> <p>If you put up more of your code, we could maybe help you adjust it. </p></pre>TapirLiu: <pre><p>It is mainly a theory problem. Assume there is a large quantity of random use inputs as the slice indexes, and my example code runs in a long loop, ....</p></pre>Ainar-G: <pre><p>User input validation and data access are two different things. Trying to merge them together will only lead to more complexity. If you really need something like that, you could just as well use a map.</p></pre>TapirLiu: <pre><p>It is just an example.</p></pre>TheMerovius: <pre><p>As len() is a builtin, it sounds more logical to me to make this a compiler-optimization, i.e. ellide the boundary-check on s[index]. Incidentally: Are you sure, this isn&#39;t happening already? Have you profiled or looked at the generated assembly? On my machine, with <a href="http://sprunge.us/jgXR" rel="nofollow">http://sprunge.us/jgXR</a> I get very fluctuating results, that I wouldn&#39;t trust to be meaningfull…</p></pre>

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

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