我有一個包含80,000多行和100列的CSV文件。我試圖以儘可能高性能的方式處理加載/訪問CSV數據。現在我的CSVParser將數據加載到一個NSArray中,但它非常緩慢/緩慢;這是一個問題,因爲我希望在移動設備上處理這種解析/加載:iPhone。加載大CSV文件時的性能問題(Objective-C)
任何建議替代方法將不勝感激。謝謝
UPDATE:
以供將來參考/討論,我現在有以下嘗試:
// Mark time the parser starts
NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
// Parse the CSV file
[parser parse];
NSTimeInterval end = [NSDate timeIntervalSinceReferenceDate];
// Print how long the parsing took
NSLog(@"raw difference: %f", (end-start));
// Copy the allLines array from the parsing delegate
NSArray *allOfTheRows = [NSArray arrayWithArray:d.allLines];
NSLog(@"There are %i lines in the csv file", [allOfTheRows count]);
NSFileManager *f = [[NSFileManager alloc] init];
NSString *filePath = @"/Users/..../rawData"; // This is of course not a literal location...
// Archive the array as NSData
NSData *someData = [NSKeyedArchiver archivedDataWithRootObject:allOfTheRows];
// Write the data to a file
[f createFileAtPath:filePath contents:someData attributes:nil];
/*
If I were to load the data from the iPhone, i'd copy the newly created someData file above to my application's mainBundle, and then unarchive the NSData to an array on the iPhone
*/
// Read the data back as an array
NSData *readData = [NSData dataWithContentsOfFile:filePath];
NSArray *bigCollectionReadBack = [NSKeyedUnarchiver unarchiveObjectWithData:readData];
我認爲這實際上取決於您如何使用數據以及數據是否能夠提供良好的替代方法。一個CSV聽起來可能不是最好的選擇(如果你有選擇) – Danny