繁体   English   中英

使用谓词进行过滤需要花费大量时间

[英]Filter using a predicate takes a lot of time

我在数组中有40k字符串。 我想过滤数组,以便仅获取匹配的字符串。 我有一些先决条件,例如它之间可以有分隔符,应该是单词搜索,搜索可以有多个单词。 因此,我使用了正则表达式,这需要很多时间。

以下是我仅出于表示目的而生成的代码。

var arr = [String]()
for index in stride(from: 0, to: 40000, by: 1) {
    arr.append("Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.")
}

// We specify the words to be searched here
let searchTexts = ["aliqua", "Ut"]

// The time the execution started
print(Date().timeIntervalSince1970)


let predicate = NSPredicate(format: "SELF matches[cd] %@", ".*\\b\(searchTexts.joined(separator: "[ ,.!?;:\"(')-]*"))\\b.*")
let fil = arr.filter { (str) -> Bool in
    return predicate.evaluate(with: str)
}

// The time the execution stopped
print(Date().timeIntervalSince1970)

在iOS模拟器中需要2秒。 它需要更多的设备。

如何改善正则表达式? 我搜索了很多网站,但并没有帮助我。

编辑:

由于涉及核心数据,因此上述问题已被修改。

我现在的实际问题是我们如何将相同的逻辑应用于核心数据提取?

如果只需要部分匹配,请不要使用需要整个字符串匹配的方法。 带有MATCHES NSPredicate需要完整的字符串匹配,并且您必须使用.*或类似的字符串来确保。 但是, .*贪婪点图案会占据整行,然后回溯以容纳后续图案的文本。 .*之后的模式越多,模式的效率就越低。

您需要使用一种允许部分匹配的方法,从而使您摆脱.* ,例如range(of:options:range:locale:)在传递.regularExpression选项的同时,指定一个range(of:options:range:locale:)

在上述情况下,您可以删除let predicate = NSPredicate(format: "SELF matches[cd] %@", ".*\\\\b\\(searchTexts.joined(separator: "[ ,.!?;:\\"(')-]*"))\\\\b.*"然后将return predicate.evaluate(with: str)替换为

return str.range(of: "\\b\(searchTexts.joined(separator: "[ ,.!?;:\"(')-]*"))\\b", options: .regularExpression) != nil

请参阅新的正则表达式演示 (56个步骤)和正则表达式演示 (541个步骤)。

暂无
暂无

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

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