NSData *data = [str dataUsingEncoding:(NSUTF8StringEncoding)];
// 获取Document目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//构造字符串文件的存储路径
NSString *strPath1 = [docPath stringByAppendingPathComponent:@"text1.txt"];
CFAbsoluteTime startTime =CFAbsoluteTimeGetCurrent();
//在这写入要计算时间的代码
[str writeToFile:strPath1 atomically:YES encoding:NSUTF8StringEncoding error:nil];
CFAbsoluteTime linkTime = (CFAbsoluteTimeGetCurrent() - startTime);
NSLog(@"C函数压缩时间 %f ms", linkTime *1000.0);
NSString *strPath2 = [docPath stringByAppendingPathComponent:@"text2.txt"];
CFAbsoluteTime startTime2 =CFAbsoluteTimeGetCurrent();
//在这写入要计算时间的代码
FILE * fp = fopen(strPath2.UTF8String, "a");
if(fp){
fwrite(str.UTF8String, strlen(str.UTF8String) + 1, 1, fp);
fclose(fp);
}
CFAbsoluteTime linkTime2 = (CFAbsoluteTimeGetCurrent() - startTime2);
NSLog(@"C函数压缩时间 %f ms", linkTime2 *1000.0);
测试结果:
不分写入的内容,前者的耗时都是后者的5-20倍之间,平均耗时为15倍
网友评论