【问题标题】:Most efficient way to compare arrays in Ruby在 Ruby 中比较数组的最有效方法
【发布时间】:2014-06-20 09:04:18
【问题描述】:

下面的代码应该可以找到arr_1arr_2 中缺少的数字。

def compare_1 (arr_1, arr_2)
    output = []

    temp = arr_2.each_with_object(Hash.new(0)) { |val, hsh| hsh[val] = 0 }

    arr_1.each do |element|
        if !temp.has_key? (element)
            output << element
        end
    end
    puts output
end
def compare_2 (arr_1, arr_2)
    out = []
    arr_1.each do |num|
        if (!arr_2.include?(num))
            out << num
        end
    end
    puts out
end

根据“基准”,第一种方法更快,大概是使用哈希。有没有更简洁的方法来编写这些或实现这一点?

compare_1 times:

    0.000000   0.000000   0.000000 (  0.003001)

compare_2 times:

    0.047000   0.000000   0.047000 (  0.037002)

【问题讨论】:

  • ?你可以做arr1 - arr2
  • array_1array_2在哪里?
  • 谢谢,我不知道我可以在 Ruby 中做到这一点。 @sawa 我已经澄清了最初的问题。

标签: ruby arrays algorithm comparison hash


【解决方案1】:

上面的代码应该找到array_1中的数字 array_2 中缺少

正如 SteveTurczyn 所说,你可以做到 array_1 - array_2

这里是Array Difference的定义

返回一个新数组,它是原始数组的副本,删除任何 也出现在 other_ary 中的项目。订单从 原始数组。

它使用哈希和 eql 比较元素?提高效率的方法。

[ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ]  #=>  [ 3, 3, 5 ]

编辑

关于性能,我通过收集该线程的信息制作了benchmark

################################################
# $> ruby -v
# ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin12.0]
################################################
require 'benchmark'

def compare_1 arr_1, arr_2
    output = []

    temp = arr_2.each_with_object(Hash.new(0)) { |val, hsh| hsh[val] = 0 }

    arr_1.each do |element|
        if !temp.has_key? (element)
            output << element
        end
    end
    output
end

def compare_2 arr_1, arr_2
    out = []
    arr_1.each do |num|
        if (!arr_2.include?(num))
            out << num
        end
    end
    out
end

require 'set'
def compare_3 arr_1, arr_2
  temp = Set.new arr_2
  arr_1.reject { |e| temp.include? e }
end

def native arr_1, arr_2
  arr_1 - arr_2
end




a1 = (0..50000).to_a
a2 = (0..49999).to_a
Benchmark.bmbm(11) do |x|
  x.report("compare_1:") {compare_1(a1, a2)}
  x.report("compare_2:") {compare_2(a1, a2)}
  x.report("compare_3:") {compare_3(a1, a2)}
  x.report("native:")    {native(a1, a2)}
end
################################################
# $> ruby array_difference.rb
# Rehearsal -----------------------------------------------
# compare_1:    0.030000   0.000000   0.030000 (  0.031663)
# compare_2:   71.300000   0.040000  71.340000 ( 71.436027)
# compare_3:    0.040000   0.000000   0.040000 (  0.042202)
# native:       0.030000   0.010000   0.040000 (  0.030908)
# ------------------------------------- total: 71.450000sec
#
#                   user     system      total        real
# compare_1:    0.030000   0.000000   0.030000 (  0.030870)
# compare_2:   71.090000   0.030000  71.120000 ( 71.221141)
# compare_3:    0.030000   0.000000   0.030000 (  0.034612)
# native:       0.030000   0.000000   0.030000 (  0.030670)
################################################

【讨论】:

    猜你喜欢
    • 2021-04-29
    • 1970-01-01
    • 2020-05-24
    • 2016-04-13
    • 2021-10-16
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    相关资源
    最近更新 更多