我的应用程序以两种方式读取所有联系人:
for循环:
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent ();
long count = macContact.addressBook.people.count;
for(int i=0;i<count;++i){
ABPerson *person = [macContact.addressBook.people objectAtIndex:i];
NSLog(@"%@",person);
}
NSLog(@"%f",CFAbsoluteTimeGetCurrent() - startTime);
对于每个
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent ();
for(ABPerson *person in macContact.addressBook.people){
NSLog(@"%@",person);
}
NSLog(@"%f",CFAbsoluteTimeGetCurrent() - startTime);
for-each 只用了 4 秒就枚举了 addressBook 中的 5000 人,而 for-loop 用了 10 分钟来完成同样的工作。
我想知道为什么性能差异很大?