1

NSNumber4 つの異なる配列で s をフォーマットして、非常に長い文字列を作成しています。

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 分かかります。

どうすればこれをもっと速くすることができますか?

4

2 に答える 2

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]];
  ...
}

これにより、多くの不要な一時的な不変の文字列の割り当てを回避できます。

于 2013-01-24T20:59:15.130 に答える
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]];
}
于 2013-01-24T20:59:21.320 に答える