Question

I was reading the NSHipster article on enumeration, which claims that for-in loops are faster than enumerateObjectsUsingBlock::

Unless you actually need the numerical index while iterating, it's almost always faster to use a for/in NSFastEnumeration loop instead.

This answer provides some rebuttal for that quote:

Fast enumeration requires translation from an internal representation to the representation for fast enumeration. There is overhead therein. Block-based enumeration allows the collection class to enumerate contents as quickly as the fastest traversal of the native storage format.

What is the translation process to move from the internal representation to the representation for fast enumeration? I understand that there is some overhead there, but how much?

Was it helpful?

Solution

The real answer: No difference that matters to pretty much any real world program. and Don't worry about it until you find an actual issue during performance quantification. and If the speed of execution of a loop matters, then your overall app architecture is likely the bug.

With that said, there is certainly some academic curiosity worth pursuing.

See:

Objective-C enumerateUsingBlock vs fast enumeration?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top