各類遍歷寫法的比較

連接:http://iosdevelopertips.com/objective-c/high-performance-collection-looping-objective-c.html
html

NSArray:ios

  • for (id object in array) :正向遍歷最快;
  • for (id object in [array reverseObjectEnumerator]) : 反向遍歷;
  • for (NSInteger i = 0; i < count; i++) : 當須要使用index或修改內容,先算出Count,再遍歷最快;
  • [array enumerateObjectsWithOptions:usingBlock:]: 須要並行遍歷時;

NSSet:objective-c

  • for (id object in set):遍歷最快;
  • for (id object in [set copy]):須要修改內容時;
  • [set enumerateObjectsWithOptions:usingBlock:] 須要並行遍歷時.

NSDictionary:oop

  • [dictionary enumerateKeysAndObjectsUsingBlock:] :遍歷最快;
  • for (id key in [dictionary allKeys]): 須要修改內容時;
  • [dictionary enumerateKeysAndObjectWithOptions:usingBlock:]: 須要並行遍歷時.
相關文章
相關標籤/搜索