簡體   English   中英

提高Ruby中數組比較的效率

[英]Improve Efficiency in Array comparison in Ruby

嗨,我正在研究Ruby / cucumber,並且需要開發一個比較模塊/程序來比較兩個文件。

以下是要求

  1. 該項目是一個遷移項目。 來自一個應用程序的數據被移至另一個

  2. 需要將現有應用程序中的數據與新應用程序中的數據進行比較。

解決方案:

我已經針對上述需求在Ruby中開發了一個比較引擎。

a)從兩個數據庫中獲取重復和排序的數據b)將數據放入帶有“ ||”的文本文件中 作為定界符c)使用在數據庫中提供唯一記錄的鍵列(數字)比較兩個文件

例如,File1有1,2,3,4,5,6,file2有1,2,3,4,5,7,而列1,2,3,4,5是鍵列。 我使用這些關鍵列並比較6和7,這將導致失敗。

問題 :

我們在這里面臨的主要問題是,如果10萬條記錄的不匹配率超過70%,那么比較時間就很大。 如果不匹配小於40%,則比較時間就可以了。

在這種情況下,Diff和Diff -LCS將不起作用,因為我們需要關鍵列才能在兩個應用程序之間進行准確的數據比較。

如果不匹配超過100,000條記錄的70%,還有其他方法可以有效地減少時間。

謝謝

假設您在2個文件中有此摘錄:

# File 1
id | 1 | 2 | 3
--------------
 1 | A | B | C
 2 | B | A | C

# File 2
id | 1 | 2 | 3
--------------
 8 | A | B | C
 9 | B | B | B

我們使用哈希 (直接訪問)執行以下功能:

def compare(data_1, data_2)
  headers = data_1.shift
  if headers.size != data_2.shift.size
    return "Headers are not the same!"
  end

  hash = {}
  number_of_columns = headers.size
  data_1.map do |row|
    key = ''
    number_of_columns.times do |index|
      key << row[index].to_s
    end
    hash[key] ||= row
  end

  data_2.each do |row|
    key = ''
    number_of_columns.times do |index|
      key << row[index].to_s
    end
    if hash[key].nil?
      # present in file 1 but not in file 2
    else
      # present in both files
    end
  end
end

# usage
data_file_1 = your_method_to_read_the_file(file_1)
data_file_2 = your_method_to_read_the_file(file_2)

compare(data_file_1, data_file_2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM