加快代码运行plist

时间:2015-06-22 14:19:29

标签: ios objective-c xcode plist

我正在用4000字的plist跑。我的代码有效,但运行代码需要几秒钟。反正是为了加速我的代码来运行plist吗?

NSString *path = [[NSBundle mainBundle] pathForResource:@"myList" ofType:@"plist"];
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
for (NSString *str in array) {
NSLog(@"%@", str);
}

提前致谢。

1 个答案:

答案 0 :(得分:4)

NSLog()很慢。删除它,只是做工作,它会更快。

以下是打印每一行与通过附加到现有字符串创建新字符串的测试比较。

   // create test array
   NSMutableArray * strings = [[NSMutableArray alloc] init];
    for (int i = 0; i < 4000; i++)
    {
        [strings addObject:@"new string"];
    }

    // create variables for storing test time. 
    CFTimeInterval startTime, endTime;

    // test the NSLog loop
    startTime = CACurrentMediaTime();
    for (NSString * string in strings) {
        NSLog(@"%@", string);
    }
    endTime = CACurrentMediaTime();
    NSLog(@"Total Runtime for NSLog: %g s", endTime - startTime);

    // test the comparison loop
    startTime = CACurrentMediaTime();
    for (NSString * string in strings) {
        NSString* newString = [string stringByAppendingString:@"   "];
    }
    endTime = CACurrentMediaTime();
    NSLog(@"Total Runtime for appending: %g s", endTime - startTime);

在我的iPhone 6上:

NSLog的总运行时间:0.55105 s

追加的总运行时间:0.00366363 s

时间测试代码是根据这篇优秀的NSHipster文章修改的: http://nshipster.com/benchmarking/