Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8220005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:22:16+00:00 2026-06-07T13:22:16+00:00

I know there are quite some topics that seem to be about the exact

  • 0

I know there are quite some topics that seem to be about the exact same thing, but I didn’t find one that really was about what I wanted.

So I was curious and wanted to compare the performance of Fast Enumeration to NSEnumerator and a for loop. (This is the part that is asked quite frequently)

First I compared Fast Enumeration:

for(NSNumber *number in testArray)
{
    assert(number);
}

NSEnumerator:

NSEnumerator *enumerator = [testArray objectEnumerator];
NSNumber *number;
while (number = [enumerator nextObject]) 
{
    assert(number);
}

for Loop:

for(NSUInteger i = 0; i < [testArray count]; i++)
{
    NSNumber *number = [testArray objectAtIndex:i];
    assert(number);
}

My testArray was an Array consisting of NSNumbers from 0 to 1,000,000 and I ran the tests 100 Times after each other and calculated the mean run time for each test.

Also I ran them on my iPad 2

Results: (mean time of all 100 runs)

  • 0.042687s Fast Enumeration
  • 0.582072s NSEnumerator
  • 0.627318s for-loop

As expected, Fast Enumeration is by far the fastest, and NSEnumerator is still a little bit faster than the for-loop, but this was for enumerating quit a large Array

So here’s the not so frequent Question:

Actually I was interested in something else: Enumeration in an array to compare each object with each other in the array

First attempt with a nested for-loop:

for(int i = 0; i < [testArray count]-1; i++)
{
    NSNumber *number = [testArray objectAtIndex:i];
    for(int j = i+1; j < [testArray count]; j++)
    {
        NSNumber *innerLoopNumber = [testArray objectAtIndex:j];
        assert(innerLoopNumber);
        assert(number);
    }
}

For these Tests I had to reduce the size of the array and the number of runs to get them done in a reasonable time, because number of iterations grows of course with O(n^2).
So I ran them with an array with 5.000 NSNumbers and repeated the tests 5 times.

Result: 7.360645s for 1 run

So I thought, sure, fast Enumeration should be faster. But to achieve the triangular Pattern to avoid comparing each pair of elements two times, I had to mix Fast Enumeration in the outer loop with NSEnumerator in the inner loop

for(NSNumber *number in testArray)
{
    NSEnumerator *reverseEnumterator = [testArray reverseObjectEnumerator];
    NSNumber *innerLoopNumber = reverseEnumterator.nextObject;
    while(innerLoopNumber && ![innerLoopNumber isEqualToNumber:number])
    {
        innerLoopNumber = reverseEnumterator.nextObject;
        assert(innerLoopNumber);
        assert(number);
    }
}

And to my surprise, this was much slower: 18.086980s for 1 run

I then tried a hybrid version as well, using Fast Enumeration for the outer loop and a for-loop for the inner one:

int counter = 0;
for(NSNumber *number in testArray)
{
    for(int j = counter +1; j < [testArray count]; j++)
    {
        NSNumber *innerLoopNumber = [testArray objectAtIndex:j];
        assert(innerLoopNumber);
        assert(number);
    }
    counter++;
}

Result: 7.079600s for 1 Run

Just slightly faster than the plain for-loop.

the numbers in one place:

  • 07.360645s for-Loop
  • 07.079600s Hybrid
  • 18.086980s Fast Enumeration

So I wonder, why is that? Does Fast Enumeration only work well when it is “undisrupted”, does the use of NSEnumerator interfere with Fast Enumeration?
Or am I just missing something and my Method is wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:22:18+00:00Added an answer on June 7, 2026 at 1:22 pm

    You’re calling additional methods in your fast enumeration loop. Objective-c has non-trivial method call overhead, so your problem is in the setup of the nested loops. As you can see fast enumeration + for loop is faster than for loop + for loop, and here you’re avoiding additional method calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there are quite a few different topics out there but I
I've been working with app engine for quite some time, I know that there
There are already quite some posts about the Singleton-Pattern around, but I would like
Having read some postings here about this topic, I realize that there are quite
I know there are already some threads about this, but I just won't work
I know there are quite some threads talking about validating XML file against its
I know there have been quite a few posts on this but none seem
I know there is a wiki page about it , but since I'm very
I know there are quite a number of posts regarding JSF paginatin here, but
I know there are similar questions to this one, but I haven't found any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.