我有一個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方法非常快。 –