Is fast evaluation really fast?

I have found over the years that readability of code has substantial value. I have also found that their are trade offs with respect to style and performance. This leads me to today’s topic that is something common to both C, Objective-C and Java. I personally find that

for(int loop=0; loop < value; loop++)

reads much better than

for(Obj *temp in array)

I find that the traditional way is more recognizable by a wider range of programmers as it is common to Java, C and Objective-C. Methods of fast evaluation differ between languages and not only are they less readable, they introduce mistakes when designers find themselves switching languages between projects.

This lead me to investigate how much faster fast evaluation really is and I found this link.

  1. objectAtIndex: enumerationUsing a for loop which increases an integer and querying the object using [myArray objectAtIndex:index] is the most basic form of enumeration.
    NSUInteger count = [myArray count];
    for (NSUInteger index = 0; index < count ; index++) {
    [self doSomethingWith:[myArray objectAtIndex:index]];
  2. NSEnumeratorThis is a form of external iteration: [myArray objectEnumerator] returns an object. This object has a method nextObject that we can call in a loop until it returns nil
    NSEnumerator *enumerator = [myArray objectEnumerator];
    id object;
    while (object = [enumerator nextObject]) {
    [self doSomethingWith:object];
  3. NSFastEnumeratorThe idea behind fast enumeration is to use fast C array access to optimize iteration. Not only is it supposed to be faster than traditional NSEnumerator, but Objective-C 2.0 also provides a very concise syntax.
    id object;
    for (object in myArray) {
    [self doSomethingWith:object];
  4. Block enumerationAvailable since the introduction of blocks, this allows to iterate an array with blocks. Its syntax isn’t as nice as fast enumeration, but there is one very interesting feature: concurrent enumeration. If enumeration order is not important and the jobs can be done in parallel without locking, this can provide a considerable speedup on a multi-core system. More about that in the concurrent enumeration section.
    [myArray enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
    [self doSomethingWith:object];
    [myArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    [self doSomethingWith:object];
    NSArray enumeration performance, iOS, logarithmic scale
    NSArray enumeration performance, iOS, linear scale
    On the iPhone 4S, the former took about 0.037 seconds while the later took about 0.140 seconds. That’s already a factor of 3.7.
    So I guess I will have to get over my issues with readability because the performance increase seems worth it
This entry was posted in General and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *