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

Ios 奇怪的测量速度:写入光盘,ios,objective-c,iphone,performance,nsfilemanager,Ios,Objective C,Iphone,Performance,Nsfilemanager,我的包里有一个.zip文件。我通过以下途径阅读: NSFileManager *fileManager = [[NSFileManager alloc] init]; fileManager.delegate = self; NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSArray *bundle

我的包里有一个.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
在CPU空闲的情况下,我甚至达到了12 MB/s


有谁能告诉我为什么复制比只保存快,为什么三个动作比七分之三快?

数学上似乎有错误。示例1:总大小=88MB*3=复制的总大小为264MB。总时间为5秒。复制速度:264/5=52.8MB/秒。其余部分也有类似的数学错误。现实检查表明“MB/s:0.017751”是错误的。为什么
createFileAtPath
会更快,可能可以通过操作系统优化来解释。也许内存利用得更好。也许这是系统使用的磁盘控制器模式。@Zaph事实上,我忘了我计算了毫秒,还以为这些是秒。我的错-谢谢。但是为什么第一次只运行3个动作比一个接一个地运行7个动作快得多呢?纠正你的问题可能是个好主意。请参阅我的其他回答。@Zaph您的其他问题讲述了
createFileAtPath:
。请注意,当我们在它之后调用
createFileAtPath:
时,该方法
copyItemAtPath:
比单独调用要慢,这让我感到奇怪。