加快循环以创建一个长字符串

时间:2013-01-24 20:52:35

标签: objective-c performance cocoa loops nsstring

我通过在四个不同的数组中格式化NSNumber来创建一个非常长的字符串:

NSString *datos = @"";

for (NSInteger counter = 0; counter < [latOut count]; counter++) {
    datos = [datos stringByAppendingString:[NSString stringWithFormat:@"%.0005f,", [[latOut objectAtIndex:counter] floatValue]]];
    datos = [datos stringByAppendingString:[NSString stringWithFormat:@"%.0005f,", [[lonOut objectAtIndex:counter] floatValue]]];
    datos = [datos stringByAppendingString:[NSString stringWithFormat:@"%ld,", [[tipoOut objectAtIndex:counter] integerValue]]];
    datos = [datos stringByAppendingString:[NSString stringWithFormat:@"%ld\n", [[velocidadOut objectAtIndex:counter] integerValue]]];
}

NSString *curDir = [[NSFileManager defaultManager] currentDirectoryPath];

NSString *path = @"";
path = [path stringByAppendingPathComponent:curDir];
path = [path stringByAppendingPathComponent:@"data.csv"];

// Y luego lo grabamos
[datos writeToFile:path atomically:YES encoding:NSASCIIStringEncoding error:&error];

计数是18,000个条目,这个循环需要将近2分钟才能完成。

我怎么能更快地做到这一点?

2 个答案:

答案 0 :(得分:4)

我在这里看到的主要建议,因为你使用字符串工作很多,就是使用NSMutableString,这应该更有效率:

// give a good estimate of final capacity
NSMutableString *datos = [[NSMutableString alloc] initWithCapacity:100000];

for (NSInteger counter = 0; counter < [latOut count]; counter++) {
  [datos appendFormat:@"%.0005f", [[latOut objectAtIndex:counter] floatValue]];
  ...
}

这将避免许多不必要的临时不可变字符串分配。

答案 1 :(得分:2)

你的记忆消耗也可能令人费解。使用可变字符串而不是创建数千个临时字符串:

NSMutableString *datos = [NSMutableString string];

for (NSInteger counter = 0; counter < [latOut count]; counter++) {
    [datos appendFormat:@"%.0005f, %.0005f, %ld, %ld\n", [[latOut objectAtIndex:counter] floatValue], 
                                                         [[lonOut objectAtIndex:counter] floatValue], 
                                                         [[tipoOut objectAtIndex:counter] integerValue], 
                                                         [[velocidadOut objectAtIndex:counter] integerValue]];
}