各种遍历写法的比较

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

NSArray:

  • for (id object in array) :正向遍历最快;
  • for (id object in [array reverseObjectEnumerator]) : 反向遍历;
  • for (NSInteger i = 0; i < count; i++) : 当需要使用index或修改内容,先算出Count,再遍历最快;
  • [array enumerateObjectsWithOptions:usingBlock:]: 需要并行遍历时;

NSSet:

  • for (id object in set):遍历最快;
  • for (id object in [set copy]):需要修改内容时;
  • [set enumerateObjectsWithOptions:usingBlock:] 需要并行遍历时.

NSDictionary:

  • [dictionary enumerateKeysAndObjectsUsingBlock:] :遍历最快;
  • for (id key in [dictionary allKeys]): 需要修改内容时;
  • [dictionary enumerateKeysAndObjectWithOptions:usingBlock:]: 需要并行遍历时.
posted @ 2014-02-13 13:43  simalone  阅读(467)  评论(0编辑  收藏  举报