本博客系列翻译自 Bigbinary 的 Ruby 2.6 系列, 已得到作者允许。Ruby 2.6.0-preview2 现已发布。
在Ruby 2.6之前, String#split
方法会返回一个被分割过的字符串数组。
在Ruby 2.6,我们可以传递代码块来枚举 String#split
的返回结果并进行操作。这样做就不用创建一个新的数组,因此内存会用得更具有效率。
接下来我们将增加一个方法 is_fruit?
来了解新的split 用法。
def is_fruit?(value)%w(apple mango banana watermelon grapes guava lychee).include?(value)
end
复制代码
输入是一组逗号连起来的蔬菜或者水果的名字,目标把它们用逗号切割并且把里头的水果保存起来。
过去的 String#split 用法
input_str = "apple, mango, potato, banana, cabbage, watermelon, grapes"splitted_values = input_str.split(", ")
=> ["apple", "mango", "potato", "banana", "cabbage", "watermelon", "grapes"]fruits = splitted_values.select { |value| is_fruit?(value) }
=> ["apple", "mango", "banana", "watermelon", "grapes"]
复制代码
Using split an intermediate array is created which contains both fruits and vegetables names.
Ruby 2.6 的可以带上代码块的 String#split 用法
fruits = []input_str = "apple, mango, potato, banana, cabbage, watermelon, grapes"input_str.split(", ") { |value| fruits << value if is_fruit?(value) }
=> "apple, mango, potato, banana, cabbage, watermelon, grapes"fruits
=> ["apple", "mango", "banana", "watermelon", "grapes"]
复制代码
当我们把代码块传递给split
方法,它会返回被分割的字符串并且不会创建一个数组。String#split
的代码块会出每一个被分割的字符串 ,这个例子里头我们往fruits 数组中推送水果名称。
后续更新
性能比较
我们创建了一个巨大无比的随机字符串来评估split 和 split + 代码块的性能差异。
require 'securerandom'test_string = ''100_000.times.each dotest_string += SecureRandom.alphanumeric(10)test_string += ' '
end
复制代码
require 'benchmark'Benchmark.bmbm do |bench|bench.report('split') doarr = test_string.split(' ')str_starts_with_a = arr.select { |str| str.start_with?('a') }endbench.report('split with block') dostr_starts_with_a = []test_string.split(' ') { |str| str_starts_with_a << str if str.start_with?('a') }endend
复制代码
结果
Rehearsal ----------------------------------------------------
split 0.023764 0.000911 0.024675 ( 0.024686)
split with block 0.012892 0.000553 0.013445 ( 0.013486)
------------------------------------------- total: 0.038120secuser system total real
split 0.024107 0.000487 0.024594 ( 0.024622)
split with block 0.010613 0.000334 0.010947 ( 0.010991)
复制代码
我们也用了benchmark/ips 来做了另一种性能测量。
require 'benchmark/ips'
Benchmark.ips do |bench|bench.report('split') dosplitted_arr = test_string.split(' ')str_starts_with_a = splitted_arr.select { |str| str.start_with?('a') }endbench.report('split with block') dostr_starts_with_a = []test_string.split(' ') { |str| str_starts_with_a << str if str.start_with?('a') }endbench.compare!
end
复制代码
结果是
Warming up --------------------------------------split 4.000 i/100mssplit with block 10.000 i/100ms
Calculating -------------------------------------split 46.906 (± 2.1%) i/s - 236.000 in 5.033343ssplit with block 107.301 (± 1.9%) i/s - 540.000 in 5.033614sComparison:split with block: 107.3 i/ssplit: 46.9 i/s - 2.29x slower
复制代码
结果看来,split 带上block 的新用法在性能上是两倍于过去的老玩法的。
这里是相关的commit 和 讨论。
原文地址
博客地址