【发布时间】:2011-05-30 18:01:15
【问题描述】:
我有一段代码返回一个表示“搜索结果”的超长字符串。每个结果都由一个双 HTML 中断符号表示。例如:
结果 1
结果 2
结果 3
我有以下循环,它获取每个结果并将其放入一个数组中,去除中断指示符“kBreakIndicator”(
)。问题是这个lopp执行时间太长了。有几个结果很好,但是一旦你达到一百个结果,它会慢 20-30 秒。这是不可接受的表现。我可以做些什么来提高性能?
这是我的代码:
content 是原始的 NSString。
NSMutableArray *results = [[NSMutableArray alloc] init];
//Loop through the string of results and take each result and put it into an array
while(![content isEqualToString:@""]){
NSRange rangeOfResult = [content rangeOfString:kBreakIndicator];
NSString *temp = (rangeOfResult.location != NSNotFound) ? [content substringToIndex:rangeOfResult.location] : nil;
if (temp) {
[results addObject:temp];
content = [[[content stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@%@", temp, kBreakIndicator] withString:@""] mutableCopy] autorelease];
}else{
[results addObject:[content description]];
content = [[@"" mutableCopy] autorelease];
}
}
//Do something with the results array.
[results release];
【问题讨论】:
标签: iphone objective-c c optimization loops