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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:01:51+00:00 2026-05-28T13:01:51+00:00

There are 2 choices (possibly more). Using NSSet as an example: NSMutableSet * mutableSet

  • 0

There are 2 choices (possibly more). Using NSSet as an example:

NSMutableSet * mutableSet = [ NSMutableSet setWithSet:nonMutableSet ] ;

or

NSMutableSet * mutableSet = [ [ nonMutableSet mutableCopy ] autorelease ] ;

Is there any difference between these two implementations? Is one “more efficient” at all? (Other examples would be NSArray/NSMutableArray and NSDictionary/NSMutableDictionary

  • 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-28T13:01:52+00:00Added an answer on May 28, 2026 at 1:01 pm

    Benchmark fun! 🙂

    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[])
    { @autoreleasepool {
        NSMutableSet *masterSet = [NSMutableSet set];
    
        for (NSInteger i = 0; i < 100000; i++) {
            [masterSet addObject:[NSNumber numberWithInteger:i]];
        }
    
        clock_t start = clock();
    
        for (NSInteger i = 0; i < 100; i++) {
            @autoreleasepool {
                [NSMutableSet setWithSet:masterSet];
            }
        }
    
        NSLog(@"a: --- %lu", clock() - start);
    
        sleep(1);
    
        start = clock();
    
        for (NSInteger i = 0; i < 100; i++) {
            @autoreleasepool {
                [[masterSet mutableCopy] autorelease]; 
            }
        }
    
        NSLog(@"b: --- %lu", clock() - start);
    
        return 0;
    } }
    

    On my machine (10.7), setWithSet: is ~3x slower than -mutableCopy (Does somebody want to try on iOS 5? 🙂 )

    Now, the question is: why?

    -mutableCopy is spending most of its time in CFBasicHashCreateCopy() (see CFBasicHash.m). This appears to be copying the hash buckets directly, with no rehashing.

    Running  Time   Self  Symbol Name
    256.0ms  61.5%  0.0   -[NSObject mutableCopy]
    256.0ms  61.5%  0.0     -[__NSCFSet mutableCopyWithZone:]
    256.0ms  61.5%  0.0       CFSetCreateMutableCopy
    255.0ms  61.2%  156.0       CFBasicHashCreateCopy
    97.0ms   23.3%  44.0          __CFSetStandardRetainValue
    

    -setWithSet is enumerating through each value of the set, and then adding it to the new set. From the implementation of CFBasicHashAddValue (again in CFBasicHash.m), it looks like it is rehashing each value in the set.

    Running    Time Self    Symbol Name
    1605.0ms   86.0%    0.0     +[NSSet setWithSet:]
    1605.0ms   86.0%    2.0       -[NSSet initWithSet:copyItems:]
    1232.0ms   66.0%    68.0        -[__NSPlaceholderSet initWithObjects:count:]
    1080.0ms   57.8%    299.0         CFBasicHashAddValue
    324.0ms    17.3%    28.0        -[NSSet getObjects:count:]
    272.0ms    14.5%    75.0          __CFBasicHashFastEnumeration
    

    This rehash makes sense at the CFSet level. CFSets take a CFSetHashCallBack in the callBacks parameter; thus, two CFSets of CFNumbers could have a different hashing routine specified. Foundation’s NSSet uses CFSet under-the-hood, and has a CFSetHashCallBack function which invokes -[NSObject hash]. (Although I guess that Apple could optimize this case and avoid the rehash when two sets have the same hash callback).

    Note that this benchmark is for NSSet (of NSNumbers) only, other collection classes may have different performance characteristics.

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

Sidebar

Related Questions

There's loads of choices for .ico files but I can't see any logic in
In the Java world, there are a few more choices for message queues, but
I'm using WatiN for web application testing. There is a field called enter choices
Is there any way that two (or more) UILocalNotifications which fires at the exact
is there any standard choice of the font family and font size for a
Look, for example at AppleScript (and there are plenty of others, some admittedly quite
cgi.escape seems like one possible choice. Does it work well? Is there something that
I have noticed in the PHP regex library there is a choice between ereg
Are there instances where switch(case) is is a good design choice (except for simplicity)
There is a conversion process that is needed when migrating Visual Studio 2005 web

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.