嘿,我是Ruby的新手,但我遇到了问题。我的文件列表中的单词超过100.000个单词,我想使用test_password方法检查我的哈希码是否等于文件列表中的单词,但是例如,当我检查文件的最后一个单词时,要花很多时间才能遍历它,请有人帮助我如何使其更快?
File.open("Wordlist.txt", "r") do |fi|
fi.each_line do |words|
text_word << words.chomp
end
end
text_word.each do |words|
if test_password(words,ARGV[0])
puts "FOUND: " + words
break
end
end
答案 0 :(得分:3)
您可以一次创建具有[hash_code(word), word]
对的哈希,然后将结果写为JSON,YAML或数据库(例如SQLite)中。
如果花很长时间来计算此哈希值是可以的,因为您只需执行一次即可。
下次,您只需要读取保存的哈希即可,这应该很快。
现在,检查哈希中是否包含单词或哈希码应该非常快。
这是一个小示例,其中还有待办事项:
require 'json'
require 'digest/md5'
hashcodes = {}
def my_hashcode(word)
Digest::MD5.hexdigest word
end
# This part is slow, that's okay because it can be saved once and for all and doesn't depend on your input
File.open('/usr/share/dict/american-english') do |wordlist|
wordlist.each do |word|
word.chomp!
hashcodes[my_hashcode(word)] = word
end
end
#TODO: Write hashcodes to JSON file
#TODO: Read hashcode from JSON file
# This part depends on your input but is very fast:
some_hashcode = my_hashcode("test")
p hashcodes[some_hashcode]
# => "test"
p hashcodes["S0MEWEIRDH4SH"]
# => nil