I feel I should first describe what I'm trying to do and then I'll ask my question(s).
I have a large amount of words (could be up to 5,000). I want to be able to display a random 3 of these words on the screen and when the user presses a button, it will display another 3 random words, but without duplicates (i.e. without presenting the same word to the user again). This will loop until a timer runs out.
If the number of words was less, I'd just add these into an NSMutableSet, use -anyObject 3 times to get the words, and then remove the 3 words from the set each time so they're not used again when I next call -anyObject. The problem with this is I don't know if I can have a set with 5,000 NSStrings in it in iOS.
So my questions are
Thanks in advance.
The main difference is that NSArray is for an ordered collection and NSSet is for an unordered collection. There are several articles out there that talk about the difference in speed between the two, like this one. If you're iterating through an unordered collection, NSSet is great.
Yes, NSArray is faster than NSSet for simply holding and iterating. As little as 50% faster for constructing and as much as 500% faster for iterating. Lesson: if you only need to iterate contents, don't use an NSSet.
NSMutableArray and NSArray both are build on CFArray , performance/complexity should be same. The access time for a value in the array is guaranteed to be at worst O(lg N) for any implementation, current and future, but will often be O(1) (constant time).
NSSet declares the programmatic interface for static sets of distinct objects. You establish a static set's entries when it's created, and can't modify the entries after that. NSMutableSet , on the other hand, declares a programmatic interface for dynamic sets of distinct objects.
Just to have an idea of which memory dimensions we are looking at: 5000 words of 8 letters each can be represented in UTF-8 (8 bits per letter) with
5000 * 8 * 8 = 320,000 bits = 40,000 bytes < 40 kB
Even if the overhead of a NSArray
, NSSet
etc. would double or triple that size (which it certainly doesn't), you'd be fine. An iPhone 4S has a RAM size of 1 GB, that's 1,048,576 kB, several ten thousand times the size of your object. In short: Don't worry.
You could store your words in a text file, separated by spaces, and simply read them into an NSArray
with
NSArray *testArray = [stringOfTextfileContent componentsSeparatedByString:@" "];
the limit depend on the amount of memory you have available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With