Reputation: 3524
I am looking for the type or data structure to store a big number of same type primitives on my app (Mac OS X or iOS) using Objective C. As I understood NSNumber stores only one primitive (correct me if I am wrong). I have, let's say, thousands of integers or strings. Which would be the best solution to put, store and access them there? NSSet, NSArray, NSMutableArray, NSDictionary, NSMutableDictionary or something else? I know that they have different features, but I care basically only about the performance of basic operations (putting, storing, retrieving).
Upvotes: 3
Views: 1875
Reputation: 5343
It only depends on how you want to ADD, STORE and REMOVE this data.
First Let us go through each type of Data Structure that is available to us in Objective-C:
Primitive Array
This is the most basic type of storage in Objective-C(or C) which is used to store primitives.
Ex: int a[4] = {1, 2, 3, 4};
The limitation to this is
NSArray
This is a container for storing objects. Any object which is of type NSObject
(or inherits from NSObject
) or is of type 'id' can be stored in NSArray
.
NSMutableArray
Same as NSArray
, but
NSSet
Same as NSArray
but
NSMutableSet
Same as NSSet
, but
NSOrderedSet
Same as NSArray
, i.e. objects are stored and retrieved by an index, but
NSMutableOrderedSet
Same as NSMutableArray
, but
NSDictionary
Can store any type of data.
NSMutableDictionary
Same as NSDictionary
This was a short description about mostly used Data Structures in Objective-C. These are used based on the need of the program and how data is to be manipulated.
Therefore,
NSMutableArray
. If you are not going to add, remove or modify any objects in the future then use NSArray
.NSOrderedSet
/NSMutableOrderedSet
NSSet
/NSMutableSet
.NSDictionary
/NSMutableDictionary
Regarding Performance
NSSet
doesn't contain any order, they are more performant than NSArray
Here is a very good and detailed article on performance characteristics for each Data Structure discussed above
Class Time [ms] 1,000,000 elements
Adding
NSMutableOrderedSet 3190.52
NSMutableDictionary 2522.47
NSMutableSet 2511.96
NSMutableArray 1423.26
NSSet 8.03
Random Access
NSMutableOrderedSet 10.74
NSMutableDictionary 9.18
NSMutableArray 8.08
NSMutableSet 4.47
NSSet 3.56
To know more about Objective-C Data Types and Data Structure, read this
Upvotes: 10