我当前的脚本:
old_ids = []
File.open("missing.txt","w") do |result|
File.open('query_result.csv', "r") do |old_copy|
old_copy.each_line do |line|
old_ids << line.chomp.to_i #add exisiting ids to array
end
end
File.open('item_info', "r") do |new_copy|
new_copy.each_line do |line|
if !old_ids.include? line.split("\t")[0].chomp.to_i #find all ids in new file that do not exist in old file and add them to missing.txt
result.puts line.split("\t")[0].chomp.to_i
puts "Adding #{line.split("\t")[0].chomp.to_i}"
end
end
end
end
如何重构它以提高效率。我解析的文件包含约 260 万条记录,因此按照书面说明,这需要很长时间才能执行。