访问数组中第一个/最后一个元素的Ruby约定 [英] Ruby convention for accessing first/last element in array

查看:116
本文介绍了访问数组中第一个/最后一个元素的Ruby约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个关于约定的问题。以下两组命令返回相同的结果。

This is a question about conventions. The two sets of commands below return identical results.

a = [1, 2, 3]

a.first  # => 1
a[0]     # => 1

a.last   # => 3
a[-1]    # => 3

在Ruby中,首选哪些是显式索引或函数?假设这是在总是访问第一个或最后一个元素的代码。

Which of these is preferred in Ruby, the explicit index or the functions? Assuming, of course, that this is in code which always accesses the first or last element.

注意:我一直在想周期。因为第一个最后接受参数,他们会有一点开销,但我不知道是否影响

Note: I've been thinking about the cycles each would take. Because first and last accept parameters, they will have a little more overhead, but I don't know if that affects what the community prefers.

感谢!

EDIT

如果你阅读了关于这篇文章的评论,对我的最后一段有一个大争论。虽然我不记得 [x] 相当于。[](x)结论第一和最后有一点开销。考虑到两者的性质,我相信这是由于参数检查第一 / 最后。这些需要检查是否有参数,而 [] 可以假设它们存在。

If you read the comments on this post, there was a big debate about my last paragraph. While I failed to remember that [x] is equivalent to .[](x), I was correct in my conclusion that first and last have a bit more overhead. Considering the nature of both, I believe that this is due to the argument check for first/last. These need to check if there are arguments whereas [] can assume that they exist.

CODE

require 'benchmark'

a = [1..1000]

MAX = 1000000

Benchmark.bm(15) do |b|
  b.report("small first") { MAX.times do; a.first; end }
  b.report("small [0]")   { MAX.times do; a[0];    end }
  b.report("small last")  { MAX.times do; a.last;  end }
  b.report("small [-1]")  { MAX.times do; a[-1];   end }
end

a = [1..100000000000]

Benchmark.bm(15) do |b|
  b.report("large first") { MAX.times do; a.first; end }
  b.report("large [0]")   { MAX.times do; a[0];    end }
  b.report("large last")  { MAX.times do; a.last;  end }
  b.report("large [-1]")  { MAX.times do; a[-1];   end }
end

RESULTS
$ b

RESULTS

                      user     system      total        real
small first       0.350000   0.000000   0.350000 (  0.901497)
small [0]         0.330000   0.010000   0.340000 (  0.857786)
small last        0.370000   0.000000   0.370000 (  1.054216)
small [-1]        0.370000   0.000000   0.370000 (  1.137655)
                      user     system      total        real
large first       0.340000   0.010000   0.350000 (  0.897581)
large [0]         0.320000   0.010000   0.330000 (  0.889725)
large last        0.350000   0.000000   0.350000 (  1.071135)
large [-1]        0.380000   0.000000   0.380000 (  1.119587)


推荐答案

TL; DR



c>和最后更少的努力,特别是一个不太经验的Ruby程序员或其他编程语言的人来理解。

TL;DR

Code is read more than it is written, and first and last take less effort, especialy for a less experienced Ruby programmer or someone from another programming language to understand.

第一 最后通常是优先的,因为他们用一点点思考传达你的意图。虽然大多数程序员会立即知道这些是相同的:

first and last are usually prefered, because they communicate your intention with a little less thinking. While most programmers will immediately know that these are the same:

a.first
a[0]

第一个仍然更容易阅读。

The first still reads more easily. There isn't a marked difference in how hard it is to read, but it's there.

最后,但是,是另一个问题。访问索引0将得到几乎所有语言的数组的第一个元素。但是负索引仅在少数几种语言中可用,并且它的使用通常不同。如果一个具有最小Ruby经验的C程序员正在试图读取我的代码,他们会更快地理解它吗?:

last, however, is another issue. Accessing the index 0 will get you the first element of an array in almost all languages. But negative indexing is only available in a few languages, and it's use often differs. If a C programmer with minimal Ruby experience is trying to read my code, which will they understand faster?:

a.last
a[-1]

负指数可能会迫使他们进行Google搜索。

The negative index will probably force them to do a Google search.

这篇关于访问数组中第一个/最后一个元素的Ruby约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆