我有30907个项目的大数据。目前,im使用NSPredicate进行过滤。但速度很慢。因此,我寻找一个最好的方式来处理他们的速度更快。
这是我当前筛选30907项的代码:
predicate = [NSPredicate predicateWithFormat:
@"((full beginswith[cd] %@) OR (full == [cd] %@) OR (search ==[cd] %@)) OR (search CONTAINS[cd] %@) OR (search beginswith[cd] %@)",matchString,matchString,matchString,strNoSpace,strNoSpace];
NSArray * arrResult = [arrSongs filteredArrayUsingPredicate:predicate];结果是正确的,但速度很慢。
发布于 2014-01-14 13:37:00
也许您可以尝试删除(full == [cd] %@),这个条件已经在完整的beginswith[cd]中评估了。换句话说,带有(full == [cd] %@)的字符串是beginswith[cd]的一个子集。
同样的事情发生在(search beginswith[cd] %@)上,它是(search CONTAINS[cd] %@)的一个子集。
所以,试试这个:
[NSPredicate predicateWithFormat:
@"((full beginswith[cd] %@) OR (search ==[cd] %@)) OR (search CONTAINS[cd] %@)",matchString,matchString,strNoSpace];发布于 2014-01-14 13:03:59
使用谓词的更好方法是..。限制数据最多的条件应排在首位。它将使您的predicate快速工作,因为下一个条件将对较少的数据进行评估。
编辑
这不仅取决于谓词,还关系到您所使用的数据结构。每个数据结构都有其搜索成本,比如在array中的搜索要比set慢。而array上的迭代要比set快。根据需求,我们应该使用数据结构。
发布于 2014-01-14 13:38:19
您可以简化条件表达式:
看一看:
((全开始matchString)或(全==cd matchString)或(搜索==cd matchString))或(搜索CONTAINScd strNoSpace)或(搜索开始使用strNoSpace)
首先,所有术语都与OR相关。您不需要括号中的括号:
(完全开始使用matchString)或(全==cd matchString)或(搜索==cd matchString)或(搜索CONTAINScd strNoSpace)或(搜索开始使用strNoSpace)
第二:如果一个字符串与存储的值匹配,它就以它开始。所以您不需要检查==
(完全开始使用matchString)或(搜索CONTAINScd strNoSpace)或(搜索开始使用OR )
第三:如果字符串以字符串开头,则包含字符串:(如果点击次数较多,则此字符串会使性能更差。(看它。)
(从matchString开始)或(搜索CONTAINScd strNoSpace)
https://stackoverflow.com/questions/21114159
复制相似问题