I am creating a very long string by formatting NSNumbers in four different arrays:
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];
The count is 18,000 entries, and this loop takes almost 2 minutes to complete.
How could I make this much faster?
Your memory consumption is also likely mindblowing. Use a mutable string instead of creating thousands of temporary strings: