通过 plist 将代码加速到 运行
Speed up code to run through 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);
}
提前致谢。
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 秒
附加的总运行时间:0.00366363 秒
定时测试代码修改自这篇优秀的 NSHipster 文章:
http://nshipster.com/benchmarking/
我正在 运行浏览一个 4000 字的 plist。我的代码有效,但需要几秒钟才能 运行 代码。有没有办法通过 plist 将我的代码加速到 运行?
NSString *path = [[NSBundle mainBundle] pathForResource:@"myList" ofType:@"plist"];
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
for (NSString *str in array) {
NSLog(@"%@", str);
}
提前致谢。
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 秒
附加的总运行时间:0.00366363 秒
定时测试代码修改自这篇优秀的 NSHipster 文章: http://nshipster.com/benchmarking/