3
我的應用程序讀取所有接觸的人有兩種方式:可可:爲什麼每個都比循環更快?
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);
的for-each
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
for(ABPerson *person in macContact.addressBook.people){
NSLog(@"%@",person);
}
NSLog(@"%f",CFAbsoluteTimeGetCurrent() - startTime);
的for-each只用了4秒鐘枚舉5000人addressBook,而for-loop花了10分鐘來完成同樣的工作。
我想知道爲什麼性能有巨大差異?
謝謝@凱文巴拉德! – NOrder