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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:31:00+00:00 2026-05-23T05:31:00+00:00

I’m coming from a C++/STL world and I wanted to check how objective-c containers

  • 0

I’m coming from a C++/STL world and I wanted to check how objective-c containers are in comparison to stl.

I wanted to compare an array of numbers but the only way to add a number to an NSArray is using NSNumber which is utterly slow and drank my ram empty so I guess I need to dealloc them manually. But I don’t want to test side effects so I just added [NSNull null] into the array.

The results of adding 10k things into array 1k times:
NSArray – 0.923411 seconds
vector<int> – 0.129984 seconds

I thought it might be allocations and deallocations so I set the number of arrays(imax in the code) to 1 and number of additions to 10000000(jmax) but it was even slower
NSArray – 2.19859 seconds
vector<int> – 0.223471 seconds

Edit:
As mentioned in the comments the constant increasing size of the array might be the problem so I made NSArray using arrayWithCapacity, but vector with reserve too and it was even slower than before(!) (imax = 1, jmax = 10000000).
NSArray – 2.55942
vector<int> – 0.19139
End edit

Why is this so slow?

My code for reference:

#import <Foundation/Foundation.h>
#include <vector>
#include <iostream>
#include <time.h>

using namespace std;

int main (int argc, const char * argv[])
{
    int imax = 1000;
    int jmax = 10000;

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    cout << "Vector insertions" << endl;

    clock_t start = clock();

    for(int i = 0; i < imax; i++)
    {
        vector<int> *v = new vector<int>();
        for(int j = 0; j < jmax; j++)
        {
            v->push_back(j);
        }
        delete v;
    }

    double interval = (clock() - start) / (double)CLOCKS_PER_SEC;

    cout << interval << " seconds" << endl;

    cout << "NSArray insertions" << endl;

    start = clock();

    for(int i = 0; i < imax; i++)
    {
        NSMutableArray *v = [[NSMutableArray alloc] init];
        for(int j = 0; j < jmax; j++)
        {
            [v addObject:[NSNull null]];
        }
        [v dealloc];
    }

    interval = (clock() - start) / (double)CLOCKS_PER_SEC;

    cout << interval << " seconds" << endl;

    [pool drain];
    return 0;
}
  • 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-05-23T05:31:01+00:00Added an answer on May 23, 2026 at 5:31 am

    @JeremyP provides an excellent link and information. Always read the fish. Here’s some breakdown of what’s eating time, though, and what you might do about it.

    First, there’s the many calls to objc_msgSend() for dynamic dispatch. These can be avoided and you’ll save some of the time (though not as much as you’d think. objc_msgSend() is crazy optimized). But you’ll knock maybe 5% off by skipping it:

      IMP addObject = class_getMethodImplementation([NSMutableArray class], @selector(addObject:));
      NSNull *null = [NSNull null];
    
      start = clock();
    
      for(int i = 0; i < imax; i++)
      {
        NSMutableArray *v = [[NSMutableArray alloc] init];
        for(int j = 0; j < jmax; j++)
        {
          addObject(v, @selector(addObject:), null);
        }
        [v release];
      }
    

    A lot of time is eaten up with retain/release. You can avoid that (and stick real numbers in rather than NSNumber) by using a non-retaining CFMutableArray). This will get the append times to about 2x of vector.

      CFArrayCallBacks cb = {0};
      for(int i = 0; i < imax; i++)
      {
        CFMutableArrayRef v = CFArrayCreateMutable(NULL, 0, &cb);
        for(int j = 0; j < jmax; j++)
        {
          CFArrayAppendValue(v, &j);
        }
        CFRelease(v);
    }
    

    The biggest cost of this one is the calls to memmove() (or the collectable version of it on the Mac).

    Man, NSMutableArray sure is slow. How could Apple be so stupid, right? I mean, really… wait… I wonder if there’s something NSMutableArray does better than vector?

    Try swapping out these lines for their obvious counterparts:

     v->insert(v->begin(), j);
    
      NSNumber *num = [[NSNumber alloc] initWithInt:j];
      [v insertObject:num atIndex:0];
      [num release];
    

    (Yes, including creating and releasing the NSNumber, not just using NSNull.)

    Oh, and you might try this one too to see just how fast NSMutableArray and CFMutableArray really can be:

      CFArrayInsertValueAtIndex(v, 0, &j);
    

    In my tests I get:

    Vector insertions
    7.83188 seconds
    NSArray insertions
    2.66572 seconds
    Non-retaining
    0.310126 seconds
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I have an array which has BIG numbers and small numbers in it. I
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.