【问题标题】:Why does the iteration take so long in Ruby?为什么在 Ruby 中迭代需要这么长时间?
【发布时间】:2019-09-16 03:08:00
【问题描述】:

嘿,我是 Ruby 新手,但遇到了问题。我的文件 Wordlist 有超过 100.000 个单词,我想使用 test_password 方法检查我的哈希码是否等于文件 Wordlist 中的一个单词,但是当我检查文件的最后一个单词时,它需要很长时间才能遍历它,请有人帮助我如何使它更快?

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

【问题讨论】:

  • 您尝试过什么调试问题?较小的输入会发生什么?哪一行需要很长时间?
  • 你可以尝试在codereview.stackexchange.com发帖
  • test_password 是做什么的?是第一次迭代还是第二次迭代很慢?
  • 第一次迭代很快,但第二次迭代很慢,test_password 检查我在命令行输入的哈希码是否等于文件中的一个单词并返回一个布尔值

标签: ruby file iteration hashcode


【解决方案1】:

您可以使用 [hash_code(word), word] 对创建一次哈希,并将结果写入 JSON、YAML 或数据库(例如 SQLite)。 如果需要很长时间来计算这个哈希值也没关系,因为你只需要做一次。 下一次,你只需要读取保存的哈希,应该很快。

现在检查一个单词或哈希码是否在哈希中应该很快。

这是一个留给你的 TODO 的小例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    相关资源
    最近更新 更多