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 9299841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:39:10+00:00 2026-06-18T22:39:10+00:00

I did some runs on my iOS app with Instruments and I saw that

  • 0

I did some runs on my iOS app with Instruments and I saw that 90% of the load on the main thread on launch (about 1000ms total) is caused by containsObject: calls. That’s on the main thread and I don’t think it’s cool.

Is there a faster alternative to this method? An algorithm or another method?

Any suggestions?

MORE INFO:

  1. I looked into my code again I realized that in fact I don’t need to know the order of the objects, only if an object is part of that set. Which means NSSet will do just fine (and I guess is faster).

  2. Number of objects – there may very well be 1000+ objects in that set.

  • 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-18T22:39:11+00:00Added an answer on June 18, 2026 at 10:39 pm

    If you NEED to use an array, skip a bit further down


    Alternate Options

    Your other options might include:

    • Using an NSDictionary which uses key->value pairs which (I expect will) have O(1) read complexity at the cost of extra storage space for the keys

    • If you aren’t using duplicates and order isn’t important, using an NSSet will offer better read complexity (I don’t know what the complexity will be, the docs probably will)


    Using an Array

    If you keep the array sorted, searching can be done in O(log n) time instead of O(n) as you can take advantage of a binary search.

    Caveat Lector: This was written from memory

    -(void) /*adding*/
    {
        int proposedIndex = 0;
        proposedIndex = [array indexOfObject:node
                                    inSortedRange:NSMakeRange(0, array.count)
                                          options:NSBinarySearchingInsertionIndex
                                  usingComparator:
                          ^ NSComparisonResult(id obj1, id obj2)
                          {
                              if (obj1.valueToCompare < obj2.valueToCompare) return NSOrderedAscending;
                              if (obj1.valueToCompare > obj2.valueToCompare) return NSOrderedDescending;
                              else return NSOrderedSame;
                          }];
    
        [array insertObject:node atIndex:proposedIndex];
    }
    
    
    -(id) /* Getting */
    {
        int location = [array indexOfObject:node
                                        inSortedRange:NSMakeRange(0, array.count)
                                              options:NSBinarySearchingFirstEqual
                                      usingComparator:
                              ^ NSComparisonResult(id obj1, id obj2)
                              {
                                  if (obj1.valueToCompare < obj2.valueToCompare) return NSOrderedAscending;
                                  if (obj1.valueToCompare > obj2.valueToCompare) return NSOrderedDescending;
                                  else return NSOrderedSame;
                              }];
        if (location == NSNotFound) return nil;
        return [array objectAtIndex:location];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I did some research and found that the only way to vertically center a
I did some changes and then did a commit. Then I realized that I
I have some code that is running in an ASP.NET application that runs a
I'm experiencing some behavior I did not expect. I have a synchronous procedure that
My iOS app runs on iOS 5 and iOS 6, but is focused on
I have a python/django application that runs on the google app engine. My views.py
I'm having some hard time running my app on Heroku - The app runs
I have some code written in C++ that compiles and runs fine (with reasonable
I have an iOS app written by someone else that I need to put
I have some code that runs fine in 2.3 and above. However, in 2.2

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.