ITeye博客

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

mirthor · 2014-02-13 14:51 · iOS性能优化 · 移动开发 · Markdown

When enumerating an NSArray:

Use for (id object in array) if enumerating forwards. Use for (id object in ) 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 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 ) if you need to modify the set (but it will be slow). Try if your code might benefit from parallel execution. When enumerating an NSDictionary

Use most of the time. Use for (id key in ) if you need to modify the dictionary. Try 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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。