数组包含其他数组的任何值吗?

测试数组是否包含第二个数组中的任何元素的最有效方法是什么?

下面的两个例子,试图回答这个问题,foods是否包含来自 cheeses的任何元素:

cheeses = %w(chedder stilton brie mozzarella feta haloumi reblochon)
foods = %w(pizza feta foods bread biscuits yoghurt bacon)


puts cheeses.collect{|c| foods.include?(c)}.include?(true)


puts (cheeses - foods).size < cheeses.size
68114 次浏览

数不胜数,有吗?

>> cheeses = %w(chedder stilton brie mozzarella feta haloumi)
=> ["chedder", "stilton", "brie", "mozzarella", "feta", "haloumi"]
>> foods = %w(pizza feta foods bread biscuits yoghurt bacon)
=> ["pizza", "feta", "foods", "bread", "biscuits", "yoghurt", "bacon"]
>> foods.any? {|food| cheeses.include?(food) }
=> true

基准脚本:

require "benchmark"
N = 1_000_000
puts "ruby version: #{RUBY_VERSION}"


CHEESES = %w(chedder stilton brie mozzarella feta haloumi).freeze
FOODS = %w(pizza feta foods bread biscuits yoghurt bacon).freeze


Benchmark.bm(15) do |b|
b.report("&, empty?") { N.times { (FOODS & CHEESES).empty? } }
b.report("any?, include?") { N.times { FOODS.any? {|food| CHEESES.include?(food) } } }
end

结果:

ruby version: 2.1.9
user     system      total        real
&, empty?         1.170000   0.000000   1.170000 (  1.172507)
any?, include?    0.660000   0.000000   0.660000 (  0.666015)
(cheeses & foods).empty?

正如 Marc-André Lafortune 在评论中所说,&是线性时间,而 any? + include?是二次的。对于较大的数据集,线性时间将更快。对于小型数据集,any? + include?可能更快,正如 Lee Jarvis 的答案所示——可能是因为 &分配了一个新的 Array,而另一个解决方案没有分配,它作为一个简单的嵌套循环来返回一个布尔值。

你可以检查十字路口是否是空的。

cheeses = %w(chedder stilton brie mozzarella feta haloumi)
foods = %w(pizza feta foods bread biscuits yoghurt bacon)
foods & cheeses
=> ["feta"]
(foods & cheeses).empty?
=> false
Set.new(cheeses).disjoint? Set.new(foods)
require "benchmark"
N = 1_000_000
puts "ruby version: #{RUBY_VERSION}"


CHEESES = %w(chedder stilton brie mozzarella feta haloumi).freeze
FOODS = %w(pizza feta foods bread biscuits yoghurt bacon).freeze


Benchmark.bm(15) do |b|
b.report("&, empty?") { N.times { (FOODS & CHEESES).empty? } }
b.report("any?, include?") { N.times { FOODS.any? {|food| CHEESES.include?(food) } } }
b.report("disjoint?") { N.times { FOODS.to_set.disjoint? CHEESES.to_set }}
end
user     system      total        real
&, empty?         0.751068   0.000571   0.751639 (  0.752745)
any?, include?    0.408251   0.000133   0.408384 (  0.408438)
disjoint?        11.616006   0.014806  11.630812 ( 11.637300)