Why is NSFastEnumeration fast?

Does anyone know if NSFastEnumeration works faster (like at runtime) than when using NSEnumerator or (for arrays) using an integer counter and loop through elements?

If it is really faster, how does it reach this speed?

Or maybe β€œfast” actually refers to faster writing of iterative code?

Thanks in advance.

+8
performance iterator objective-c cocoa fast-enumeration
source share
2 answers

Depending on the container, NSFastEnumeration is executed at least in the same way as NSEnumerator , but usually much faster, since it requires fewer method calls.

Instead of calling nextObject for each element of the array, with NSFastEnumerator array can return a block of objects in array C immediately. Iterating this C array is much faster than making many method calls, each of which searches and returns only one object.

+6
source share

Fast enumeration not faster than any for loop. In a general sense, this is not possible, since the base loops are already optimized as they can be.

Instead, Fast enumeration is faster than a "for" or "while" loop, which extracts data at each iteration from an Objective-C object by calling the Objective-C method.

He does this by receiving data in packets, reducing the overhead of calling the method at each iteration. Removing a method call from the inside of the loop also has the benefits of optimizing the compiler.

+5
source share

All Articles