Brent Simmons talks about for-in loops in a recent post. In it he says:
most of the time the for-in enumeration is the straightforward and less clever approach. (“Less clever” is a good thing.)
I agree with Brent. I’m a fan of the for-in syntax. It’s straightforward and easy to read. But I have to admit in recent years I’ve been using enumerateObjectsUsingBlock:
more often than for-in loops. I’ve been favoring the block syntax because something bbum said on Stack Overflow a while back.
enumerateObjectsUsingBlock:
will be as fast or faster than fast enumeration (for(... in ...
) uses theNSFastEnumeration
support to implement enumeration). 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. Likely irrelevant for arrays, but it can be a huge difference for dictionaries.
For those who don’t know who bbum is, he’s Bill Bumgarner. He works at Apple and knows a thing or two about Cocoa. So I believe him when he says “enumerateObjectsUsingBlock:
will be as fast or faster than fast enumeration.”
Posted in programming. Tagged in ios, os x.
Related Articles
- No WWDC Or SF Trip
- Après Code: A New Developer Meetup In Stowe Vt
- Dispatch Async to Main Queue Doesn't Work With Modal Window on Mac OS X
- Recommended Software for Mac
- A Review of CVS Clients for Mac OS X
- My Recommended Mac OS X Applications
- Cannot Resolve Machine Name From Mac OS X
- How To Access a Windows 2003 File Share From Mac OS X
- Excited About Expected New Arrival
- Finally Windows on Mac