简体   繁体   English

需要帮助加快计算

[英]Need help speeding up a calculation

I wrote a program that generates a md5 hash onto a printed out bill.我写了一个程序,在打印出来的账单上生成一个 md5 哈希值。 I want to be able to check the hash against a generated list of hashes.我希望能够根据生成的哈希列表检查哈希。 I then use a Levenshtein distance function to figure out which hash has the lowest edit distance from the printed out bill.然后我使用 Levenshtein 距离函数来确定哪个哈希与打印出的账单的编辑距离最小。

Here is my code:这是我的代码:

func checkIfBillIsLegit(stringToCheck:String) -> Bool {
  for i in 0...((secretWords.count)) {                                 // for loop runs about 5 times
     let hashs = String().generateAll(secretWords[i])                  // create the md5 hashs to check against, returns an array with 50 elements
     for j in 0...(hashs.count) {
        if (stringToCheck.minimumEditDistance(hashs[j]) < 5) {        // Levenshtein distance function
           print("legit")
           print(secretWords[i])
           return true
        }
     }
  }

  print("not legit")
  return false
}

I want to be able to run this method multiple times per second.我希望能够每秒多次运行此方法。 It works now, but it's slightly too slow for what I want to do.它现在可以工作,但对于我想做的事情来说有点太慢了。 Problem is, the generateAll() method is too slow to generate 50 hashes per second.问题是, generateAll() 方法太慢,无法每秒生成 50 个哈希值。 I was thinking of calling generateAll outside of this method, but I can't figure out how I would be able to keep track of the list?我想在此方法之外调用 generateAll,但我不知道如何跟踪列表?

Any help would be appreciated.任何帮助,将不胜感激。

generateAll() method: generateAll() 方法:

You can use an NSCache to ensure that you only calculate the hashes for a particular word when required.您可以使用NSCache来确保仅在需要时计算特定单词的哈希值。 Typically this will be the first time your function is called, but it could also be if the array of secret words is expanded:通常这将是您的函数第一次被调用,但也可能是秘密词数组被扩展时:

var hashCache = NSCache()

func checkIfBillIsLegit(stringToCheck:String) -> Bool {

    for secretWord in secretWords {                                 
        var hashes = hashCache.objectForKey(secretWord) as? [Hash]
        if hashes == nil {
            hashes= String().generateAll(secretWord)
            hashCache.setObject(hashes, forKey: secretWord)
        }

        for hash in hashes! {
            if stringToCheck.minimumEditDistance(hash) < 5 {
                print("legit")
                print(secretWord)
                return true
            }
        }
    }
    print("not legit")
    return false
}

If you want to know which "secret word" was the match, then I would change the function to return String?如果您想知道匹配的是哪个“秘密词”,那么我会将函数更改为返回String? :

var hashCache = NSCache()

func checkIfBillIsLegit(stringToCheck:String) -> String? {

    for secretWord in secretWords {                                 
        var hashes = hashCache.objectForKey(secretWord) as? [Hash]
        if hashes == nil {
            hashes= String().generateAll(secretWord)
            hashCache.setObject(hashes, forKey: secretWord)
        }

        for hash in hashes! {
            if stringToCheck.minimumEditDistance(hash) < 5 {
                print("legit")
                print(secretWord)
                return secretWord
            }
        }
    }
    print("not legit")
    return nil
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM