Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS how large can an NSSet/NSArray/NSDictionary be?

I feel I should first describe what I'm trying to do and then I'll ask my question(s).

Background

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.

Possible solution

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.

Question

So my questions are

  1. Can I have a collection object (NSSet, NSArray, NSDictionary) with about 5,000 strings in it in iOS without any performance problems?
  2. If not how would I go about reading a subset of strings from a file to get an acceptable array size and then pull out more when I've emptied that array?
  3. What would be the best way to store these strings? They're only words, so not very long ones. I was thinking of just adding them to a file with a string on each line and reading them into a collection object on application load.

Thanks in advance.

like image 540
Grant J Avatar asked Jun 03 '12 08:06

Grant J


People also ask

What's a difference between NSArray and NSSet?

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.

Is it faster to iterate through an NSArray or an NSSet?

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.

Which is faster NSArray or NSMutableArray?

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).

What is NSSet in Objective C?

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.


2 Answers

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:@" "];
like image 187
Stefan Avatar answered Oct 13 '22 00:10

Stefan


the limit depend on the amount of memory you have available.

like image 22
iArezki Avatar answered Oct 13 '22 00:10

iArezki