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

  • SEARCH
  • Home
  • 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 8422927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:35:29+00:00 2026-06-10T03:35:29+00:00

My application read all people in contact in two ways: for-loop: CFAbsoluteTime startTime =

  • 0

My application read all people in contact in two ways:

for-loop:

    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent ();
    long count = macContact.addressBook.people.count;
    for(int i=0;i<count;++i){
        ABPerson *person = [macContact.addressBook.people objectAtIndex:i];
        NSLog(@"%@",person);
    }
    NSLog(@"%f",CFAbsoluteTimeGetCurrent() - startTime);

for-each

    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent ();
    for(ABPerson *person in macContact.addressBook.people){
        NSLog(@"%@",person);
    }
    NSLog(@"%f",CFAbsoluteTimeGetCurrent() - startTime);

for-each only took 4 seconds to enumerate 5000 people in addressBook, while for-loop took 10 minutes to do the same job.

I want to know why there is a huge difference in performance?

  • 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-10T03:35:31+00:00Added an answer on June 10, 2026 at 3:35 am

    The difference in performance almost certainly has to do with the macContact.addressBook.people part. You’re calling that every single time through the for loop but only once with the for-each loop. I’m guessing either the addressBook or the people properties are not returning cached data but rather new data every time.

    Try using

    NSArray *people = macContact.addressBook.people;
    for (int i = 0; i < [people count]; i++) {
        NSLog(@"%@", [people objectAtIndex:i];
    }
    

    You’ll probably find the performance is very similar again.


    That said, for-each is faster than for in general. The reason is because a for loop invokes a method send on every single pass through the loop (-objectAtIndex:), whereas for-each can fetch the objects much more efficiently by grabbing them in large batches.

    In more recent versions of the OS you can go a step further and use a block-based enumeration method. This looks like

    [macContact.addressBook.people enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL stop){
        NSLog(@"%@", obj);
    }];
    

    For NSArrays this should have very similar performance to a for-each loop. For other data structures such as dictionaries this style can be faster because it can fetch the value along with the key (whereas a for-each only gives you the key and requires you to use a message send to get the value).

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

Sidebar

Related Questions

I created one application to read all the contacts stored in android 2.0 emulator.I
I'm trying to have my application read through a text file and look for
i have developed application for read information from card reader. Here i have used
I have my application to read and display an ePub file almost ready. What
I have developed an application that read how many files are there in a
I'm making chat application and read messages data from SQLite database. I want to
I need to write a small application to read a configuration file and generate
I want to write a file where an external application can read it, but
I am using WIC (Windows Imaging Component) in my WPF application to read/write image
I have read about application object in android site but I couldn't understand. What

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.