我有一个NSMutableDictionary包含MPMediaItem和它的标题的字符串是它的关键。我目前在词典中有1,777项。优化循环与NSMutableDictionary
我正在循环查找与提供的NSString模糊匹配的字典。我如何加快速度?每次运行大约需要6秒。
我就在循环本身过去
@autoreleasepool {
float currentFoundValue = 1000.0;
NSMutableArray *test;
MPMediaItemCollection *collection;
float match;
for(id key in artistDictionary)
{
NSString *thisArtist = key;
int suppliedCount = [stringValue length];
int keyCount = [thisArtist length];
if(suppliedCount > keyCount)
{
match = [StringDistance stringDistance:thisArtist :stringValue];
} else {
match = [StringDistance stringDistance:stringValue :thisArtist];
}
if(match < currentFoundValue)
{
currentFoundValue = match;
test = [artistDictionary objectForKey:thisArtist];
collection = [[MPMediaItemCollection alloc] initWithItems:test];
}
}
...
我发现objectForKey是一个糟糕的罪魁祸首这里。 stringDistance方法非常快。 –