`
mirthor
  • 浏览: 1743 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

iOS性能优化之一:在Objective-C中遍历集合的性能

阅读更多
When enumerating an NSArray:

Use for (id object in array) if enumerating forwards.
Use for (id object in [array reverseObjectEnumerator]) if enumerating backwards.
Use for (NSInteger i = 0; i < count; i++) if you need to know the index value, or need to modify the array.
Try [array enumerateObjectsWithOptions:usingBlock:] if your code might benefit from parallel execution.
When enumerating an NSSet:

Use for (id object in set) most of the time.
Use for (id object in [set copy]) if you need to modify the set (but it will be slow).
Try [set enumerateObjectsWithOptions:usingBlock:] if your code might benefit from parallel execution.
When enumerating an NSDictionary

Use [dictionary enumerateKeysAndObjectsUsingBlock:] most of the time.
Use for (id key in [dictionary allKeys]) if you need to modify the dictionary.
Try [dictionary enumerateKeysAndObjectWithOptions:usingBlock:] if your code might benefit from parallel execution.
Not only are these methods the fastest available, but they’re also all very clear and readable. So remember, sometimes it’s not a choice between writing clean code and fast code; you may find that you can get the best of both worlds.


from:Nick Lockwood  http://iosdevelopertips.com/objective-c/high-performance-collection-looping-objective-c.html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics