奇怪的测量速度:写入光盘

时间:2014-10-16 14:04:20

标签: ios objective-c iphone performance nsfilemanager

我的捆绑包里有一个.zip文件。我通过以下方式阅读:

NSFileManager *fileManager = [[NSFileManager alloc] init];
fileManager.delegate = self;

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSArray *bundleZipFile = [[NSBundle mainBundle] pathsForResourcesOfType:@"zip" inDirectory:nil];
NSString *filePath = [bundleZipFile firstObject];
NSData *file =  [fileManager contentsAtPath:filePath];

if ([fileManager fileExistsAtPath:filePath] && file) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
        NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil];
        double fileSizeInBytes = [attributes[NSFileSize] doubleValue];
        NSDate *startDate = [NSDate date];

        [fileManager copyItemAtPath:filePath toPath:copy1FilePath error:nil];
        double timePassed1Ms = [startDate timeIntervalSinceNow] * -1000.0;

        [fileManager copyItemAtPath:copy1FilePath toPath:copy2FilePath error:nil];
        double timePassed2Ms = [startDate timeIntervalSinceNow] * -1000.0;

        [fileManager copyItemAtPath:copy2FilePath toPath:copy3FilePath error:nil];
        double timePassed3Ms = [startDate timeIntervalSinceNow] * -1000.0;

        [fileManager createFileAtPath:copy4FilePath contents:file attributes:nil];
        double timePassed4Ms = [startDate timeIntervalSinceNow] * -1000.0;

        [fileManager createFileAtPath:copy5FilePath contents:file attributes:nil];
        double timePassed5Ms = [startDate timeIntervalSinceNow] * -1000.0;

        [fileManager createFileAtPath:copy6FilePath contents:file attributes:nil];
        double timePassed6Ms = [startDate timeIntervalSinceNow] * -1000.0;

        [file writeToFile:copy7FilePath atomically:YES];
        double timePassed7Ms = [startDate timeIntervalSinceNow] * -1000.0;

        dispatch_async(dispatch_get_main_queue(), ^(void) {
             // here I calculate times like
             double bytesPerSecond = fileSizeInBytes / timePassed3Ms;
             double MBps = bytesPerSecond / 1000000.0;
        });
    });
}

我想在iOS上测试保存速度。看到结果后,我很困惑:

FileSize: 88375520.000000    

Method: copyItemAtPath:toPath:error:
Copying 1 took: 1460.937977 milliseconds
Copying 2 took: 1770.565987 milliseconds
Copying 3 took: 1747.061014 milliseconds
Total copying took: 4978.564978
Bytes per second: 17751.203489
MB/s: 0.017751

Method: createFileAtPath:contents:attributes:
Save 1 took: 3752.479970 milliseconds
Save 2 took: 2313.687027 milliseconds
Save 3 took: 3230.316997 milliseconds
Total saving took: 9296.483994
Bytes per second: 9506.338102
MB/s: 0.009506

Method: writeToFile:atomically:
Bytes per second: 32301.891362
MB/s: 0.032302

所以看来,将数据保存在缓冲区并且仅保存它比从文件读取并保存到另一个文件要慢:/。更重要的是,我试图只运行3个第一个动作(只有copyItemAtPath:toPath:error :)并且结果要好得多:

FileSize: 88375520.000000    
Method: copyItemAtPath:toPath:error:
Copying 1 took: 14.973998 milliseconds
Copying 2 took: 3.435016 milliseconds
Copying 3 took: 2.867997 milliseconds
Total copying took: 21.277010
Bytes per second: 4153568.483964
MB/s: 4.153568

我甚至在这里实现了~12 MB / s,而且CPU是免费的。

任何人都可以告诉我为什么复制比仅保存更快以及为什么3个动作比7个中的3个快...?

0 个答案:

没有答案