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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:27:51+00:00 2026-06-03T22:27:51+00:00

Continuing off this post: Performance hit incurred using NSMutableDictionary vs. NSMutableArray> I am trying

  • 0

Continuing off this post: Performance hit incurred using NSMutableDictionary vs. NSMutableArray>

I am trying to run a little test to see if the performance gap is that great for read and writes between NSArray & NSDictionary as well as their mutable coutnerparts…

However, I am having difficulties finding a “balanced” test… because the dictionary has 2 (or 3 depending on how you see this) objects to loop through to get the value (not the key) seeked, while the array has only one…

Any suggestions?

—If you want more details:
What I mean is easier to explain through examples;

For the array:
(for NSString *str in array) { do smth with the string }

For the dictionary

(for NSString *str in [dictionary allValues]) { string }

OR

(for NSString *str in [dictionary allKeys]) { [dictionary valueForKey:key] }

OR

(for NSString *str in [dictionary allKeys]) { string }

OR EVEN

NSArray *valuesOrKeys = [dictionary allKeys/allValues];

(for NSString *str in valuesOrKeys) {string }

What is the “fairest” test to do for the dictionary?

–EDIT (comment)

As you all pointed (and asked why I would want that) that when a dictionary is used, it’s because it fits the model better than an array…

well the reason for my asking is that an app I’m building is painfully slow and so I’m trying to figure out if the use of a different datatype would change any of that, and I am considering using basic c arrays… I have the choice at this point so I am able to change the inner workings to fit whatever type I want…

  • 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-03T22:27:52+00:00Added an answer on June 3, 2026 at 10:27 pm

    I’d like to point you at the following article: “Array“, by ridiculous_fish, an engineer at Apple. Cocoa arrays are not necessarily well-implemented naïve arrays as you might expect, nor are dictionaries simple hash tables. Their performance is very circumstantial, and depends on the number of objects they hold (as well as their values, etc.). This might not directly affect the answer, but it’s something to consider (NSDictionary performance will, of course, vary with the speed and reliability of your hashing function, and so on).

    Additionally, if you’re looking for a ‘balanced’ test, you’d have to look for a way for both classes to behave as close to each other as possible. You want to rule out accessing values via keys in the dictionary, because that — regardless of how fast seek times are for the underlying data structures maintained by NSDictionary — is slower than simply pulling objects from an array because you’re performing more operations to do it. Access from an array is O(1), for a hash table, O(1) at best and O(n) at worst (depending on the implementation, somewhere in the middle).

    There are several ways to enumerate both dictionaries and arrays, as you mentioned above. You’re going to want to use the methods that are closest to each other in terms of implementation, those being either block-based enumeration (enumerateObjectsUsingBlock: for NSArray and enumerateKeysAndObjects: for NSDictionary), or fast enumeration (using either allKeys or allValues for the NSDictionary). Because the performance of these algorithms is mainly empirical, I performed several tests to note access times (each with 10000 NSNumber objects):

    NSArray, Block Enumeration:
    1. 10.5s
    2.  9.1s
    3. 10.0s
    4.  9.8s
    5.  9.9s
       -----
        9.9s Avg
    
    NSArray, Fast Enumeration:
    1.  9.7s
    2.  9.5s
    3.  9.3s
    4.  9.1s
    5. 10.5s
       -----
        9.6s Avg
    
    NSDictionary, Block Enumeration
    1. 10.5s
    2. 10.6s
    3.  9.9s
    4. 11.1s
    5. 11.0s
       -----
       10.6s Avg
    
    NSDictionary, allKeys -> Fast Enumeration
    1. 10.0s
    2. 11.2s
    3. 10.2s
    4. 10.8s
    5. 10.8s
       -----
       10.6s Avg
    
    NSDictionary, allValues -> Fast Enumeration
    1. 10.7s
    2. 10.3s
    3. 10.5s
    4. 10.5s
    5.  9.7s
       -----
       10.3s Avg
    

    As you can see from the results of this contrived test, NSDictionary is clearly slower than NSArray (around 7% slower using block enumeration, and 7–10% slower with fast enumeration). However, this comparison is rather pointless, seeing as using the fastest enumeration for NSDictionary simply devolves it into an array anyway.

    So the big question is, why would you consider using a dictionary? Arrays and hash tables aren’t exactly interchangeable; what kind of model do you have that allows drop-in replacement of NSArray with NSDictionary? Regardless of the times given by contrived examples to prove performance benefits one way or another, you should always implement your models in a way that makes sense — you can optimize later for performance if you have to. I don’t see how you would uses these data structures interchangeably, but anyway, NSArray is the winner here, especially considering the sequential order in which you’re attempting to access values.

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

Sidebar

Related Questions

Continuing from this question : When I am trying to do fopen on Windows,
This post says that it is possible to turn off the CLR flag for
Referring to this post and this one . I'm trying to implement tag search
Continuing on my attempt to create a DateTime class , I am trying to
Continuing my quest to perform a dropbox query I am using http-conduit to try
Continuing from this question With each system call, the function constructs a set of
First off, don't link to the Don't parse HTML with Regex post :) I've
This is my first time using JQuery in any of my projects. I have
I am aware of this other post , but it doesn't seem to apply
Long post - sorry.... I'm doing input validation for a WCF service and using

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.