速查 NSArray NSSet NSHashTable 快速遍歷之速度比較

由於NSArray中的指針並非簡單的連續存放的,因此簡單的測試了Cocoa的三種集合的快速遍歷(NSFastEnumeration)性能,給出簡單的參考。數組

添加元素:性能

[collection addObject:[NSObject new]];

遍歷工做:測試

for (id item in collection.objectEnumerator) {
    [item isProxy];
}

循環次數:1,000,000spa

 

樣本:指針

NSMutableArray;
//0.026123/0.028087/0.031107/0.026678/0.024862/0.023245

NSMutableSet;
//0.044908/0.026870/0.027532/0.035661/0.034451/0.027642

NSHashTable;
//0.130406/0.128523/0.116676/0.117398/0.121508/0.113544

HashTable最差,慢4倍左右;code

數組最穩定,Set不穩定標準差大,數組微勝。blog

結論:for in中仍是數組有優點。it

相關文章
相關標籤/搜索