更喜欢 %w(...) 而不是文字数组? [英] Prefer %w(...) to a literal array?

查看:55
本文介绍了更喜欢 %w(...) 而不是文字数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在向 RubyMine 输入一个简单的字符串文字数组时:

While entering a simple literal array of strings in to RubyMine:

cols =
[
  "Col1",
  "Col2"
]

RubyMine 抱怨我更喜欢使用 %w 而不是文字数组:

RubyMine complained that I should prefer to use %w over a literal array:

cols = %w(Col1 Col2)

它提供了一个指向 Ruby 样式指南的链接(此处),其中说:

It presented a link to a Ruby Style Guide (here), which said:

当你需要一个数组时,更喜欢 %w 而不是文字数组语法字符串.

Prefer %w to the literal array syntax when you need an array of strings.

# bad
STATES = ['draft', 'open', 'closed']

# good
STATES = %w(draft open closed)

我可以看到 %w 如何提供可能更简洁的代码.除了简洁之外,是否有任何理由更喜欢一种方法而不是另一种方法?

I can see how the %w provides potentially more concise code. Aside from brevity, is there any reason to prefer one method over the other?

推荐答案

瞧!,一个基准:

require 'benchmark'

n = 1_000_000
Benchmark.bm(11) do |b|
  b.report('%w') { n.times { %w[a b c d e f g h i j k l m n o p q r s t u v w x y z] } }
  b.report('explicit') { n.times { ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] } }
  b.report('numerics') { n.times { [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26] } }
end

                  user     system      total        real
%w            2.590000   0.000000   2.590000 (  2.591225)
explicit      2.590000   0.000000   2.590000 (  2.584781)
numerics      0.300000   0.000000   0.300000 (  0.309161)

                  user     system      total        real
%w            2.590000   0.000000   2.590000 (  2.591516)
explicit      2.590000   0.000000   2.590000 (  2.584155)
numerics      0.300000   0.000000   0.300000 (  0.308896)

                  user     system      total        real
%w            2.590000   0.000000   2.590000 (  2.592848)
explicit      2.590000   0.000000   2.590000 (  2.585558)
numerics      0.300000   0.000000   0.300000 (  0.308570)

我添加了数字"数组测试,因为我怀疑 %w 由于测试字符串性而比使用显式字符串更快.%w 不需要这样做,因为它假设一切都是字符串.运行三遍后,就处理字符串而言,这是一次清洗.数字规则、字符串流口水等等.

I added the "numerics" array test because I suspected that %w is faster than using explicit strings due to testing for stringiness. %w doesn't need to do that, because it assumes everything is a string. After running it three times it's a wash as far as dealing with strings. Numbers rule, strings drool, and all that.

之前的基准测试是在我的系统上使用 Ruby 1.9.3-p286 运行的.我在家里用我的旧 MacBook Pro 再次测试,使用 Ruby 1.8.7-p358,因此由于托管硬件的差异,加上运行较旧的 Ruby,以下数字较慢:

The previous benchmarks were run using Ruby 1.9.3-p286 on my system at work. I tested again using my old MacBook Pro at home, using Ruby 1.8.7-p358, so the following numbers are slower due to the differences in the hosting hardware, plus running an older Ruby:

                user     system      total        real
%w           3.070000   0.000000   3.070000 (  3.080983)
explicit     3.100000   0.000000   3.100000 (  3.093083)
numerics     0.950000   0.040000   0.990000 (  0.990535)

                user     system      total        real
%w           3.080000   0.010000   3.090000 (  3.076787)
explicit     3.090000   0.000000   3.090000 (  3.089246)
numerics     0.950000   0.030000   0.980000 (  0.989579)

                user     system      total        real
%w           3.080000   0.000000   3.080000 (  3.073877)
explicit     3.090000   0.000000   3.090000 (  3.091576)
numerics     0.950000   0.030000   0.980000 (  0.989132)

在 1.8.7 上,%w 始终比之前快一点,这可能引起了速度谣言.

On 1.8.7, %w was a tiny bit faster consistently, which probably gave rise to the speed-rumors.

这篇关于更喜欢 %w(...) 而不是文字数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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