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

WhenenumeratinganNSArray:

Usefor(idobjectinarray)ifenumeratingforwards.

Usefor(idobjectin[arrayreverseObjectEnumerator])ifenumeratingbackwards.

Usefor(NSIntegeri=0;i<count;i++)ifyouneedtoknowtheindexvalue,orneedtomodifythearray.

Try[arrayenumerateObjectsWithOptions:usingBlock:]ifyourcodemightbenefitfromparallelexecution.

WhenenumeratinganNSSet:

Usefor(idobjectinset)mostofthetime.

Usefor(idobjectin[setcopy])ifyouneedtomodifytheset(butitwillbeslow).

Try[setenumerateObjectsWithOptions:usingBlock:]ifyourcodemightbenefitfromparallelexecution.

WhenenumeratinganNSDictionary

Use[dictionaryenumerateKeysAndObjectsUsingBlock:]mostofthetime.

Usefor(idkeyin[dictionaryallKeys])ifyouneedtomodifythedictionary.

Try[dictionaryenumerateKeysAndObjectWithOptions:usingBlock:]ifyourcodemightbenefitfromparallelexecution.

Notonlyarethesemethodsthefastestavailable,butthey’realsoallveryclearandreadable.Soremember,sometimesit’snotachoicebetweenwritingcleancodeandfastcode;youmayfindthatyoucangetthebestofbothworlds.

from:NickLockwoodhttp://iosdevelopertips.com/objective-c/high-performance-collection-looping-objective-c.html