ios - What is the most reliable and fastest type or data structure to store data using Objective C? -
i looking type or data structure store big number of same type primitives on app (mac os x or ios) using objective c. understood nsnumber stores 1 primitive (correct me if wrong). have, let's say, thousands of integers or strings. which best solution put, store , access them there? nsset, nsarray, nsmutablearray, nsdictionary, nsmutabledictionary or else? know have different features, care performance of basic operations (putting, storing, retrieving).
it depends on how want add, store , remove data.
first let go through each type of data structure available in objective-c:
primitive array
this basic type of storage in objective-c(or c) used store primitives. ex: int a[4] = {1, 2, 3, 4};
limitation
- can store primitive types.
- array size cannot changed once declared.
- can retrieved index.
- can store single type of data, defined @ time of declaring array.
nsarray
this container storing objects. object of type nsobject
(or inherits nsobject
) or of type 'id' can stored in nsarray
.
- once initialized, cannot mutated i.e. array size cannot changed nor objects contains can modified. in terms of security.
- objects can accessed index.
nsmutablearray
same nsarray
, but
- can mutated, i.e. existing objects can modified , new objects can added or deleted.
nsset
same nsarray
but
- stores unique objects.
- objects cannot accessed index. objects can accessed enumeration.
nsmutableset
same nsset
, but
- can mutated, i.e. objects can added or removed @ later point of time.
nsorderedset
same nsarray
, i.e. objects stored , retrieved index, but
- stores unique objects.
nsmutableorderedset
same nsmutablearray
, but
- stores unique objects.
nsdictionary
can store type of data.
- objects stored , retrieved key.
- once initialized, cannot mutated i.e. cannot add new key-values nor can update existing objects associated particular key.
nsmutabledictionary
same nsdictionary
- can mutated, i.e. new objects can added or removed , existing objects can modified.
this short description used data structures in objective-c. these used based on need of program , how data manipulated.
therefore,
- if want store thousands of numbers , strings , want access index value use
nsmutablearray
. if not going add, remove or modify objects in future usensarray
. - if want store data not want duplicates , want access index use
nsorderedset
/nsmutableorderedset
- if want store data not want duplicates , order doesn't matter use
nsset
/nsmutableset
. - if want access data particular key use
nsdictionary
/nsmutabledictionary
regarding performance
- since
nsset
doesn't contain order, more performantnsarray
here , detailed article on performance characteristics 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 objective-c data types , data structure, read this
Comments
Post a Comment