开发者

Objective C -- property lists or text files?

开发者 https://www.devze.com 2022-12-17 05:25 出处:网络
I need to import a list of about 40,000 words into my Iphone app. 开发者_如何学Python The list will be the same every time the app starts.It seems that property lists and text files are reasonable opt

I need to import a list of about 40,000 words into my Iphone app. 开发者_如何学Python The list will be the same every time the app starts. It seems that property lists and text files are reasonable options.

Any reason to prefer one over the other? For reasons I don't understand, finder says the property list on my mac is 1MB, while the text file is only 328K. The property list is an NSMutableArray of NSMutableArrays of NSStrings. The text file is a plain txt file. But amount of time the app takes to start up is also important. If I read in a text file, my app would have to do some simple processing on it each time it starts.

Thanks.


Every array is surrounded by <array></array> and every string is surrounded by <string></string> in XML property lists. I don't know what a "plain text file" is in the context of storing an array, but I imagine you're talking about something less verbose than XML. Over that amount of data, I could see XML taking three times as much space as, say, CSV. May as well go for the smaller format if it does the job as well.


What Justin said -- measure first and then optimize if the simplest solution -- the property lists -- are too costly. Note also the property lists can be binary; see the plutil man page.

Property lists aren't magic; they have to be parsed and processed on start, too.

Thus, your best bet is to shove 'em all into a text file, one word per line.

Now, if you want to be super efficient about it, I would do something like:

  • use NULL characters as your delimiter

  • allocate a buffer of memory the size of the text file and read the entire contents of the file into it (you could use NSMutableData's +dataWithContentsOfFile: quite easily, then just call -bytes to get the writable buffer).

  • you'll obviously need a pointer to each word; trivial -- iterate through the buffer character by character and, every time you see a NULL (and aren't at the end), you know the next byte -- the next address -- will be a pointer to the first character of the next word

  • if you need NSStrings, use NSString's [initWithBytesNoCopy:length:encoding:freeWhenDone:][2]

Easy enough that I wouldn't even count it as premature optimization.

If you wanted to get really tricky, you could sort the words by length and do a bit of creative pointer arithmetic to avoid iterating over every character. But that would be premature.


Try both and see if the speed is an issue. It's entirely possible that you won't notice a difference, in which case I'd go with XML, to avoid the 'simple processing' you'd have to do on the text file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号