Re: Reading in dictionary from txt file: options for speed
Re: Reading in dictionary from txt file: options for speed
- Subject: Re: Reading in dictionary from txt file: options for speed
- From: Miles <email@hidden>
- Date: Thu, 16 Apr 2009 18:30:33 -0700
I'm creating a game for where the dictionary file will never get modified,
so I'm not really worried about that. The strings that I am searching for
are not from user input, but from user selection, so I'm guaranteed to have
the case be correct -- so I don't need to worry about upper vs lower-case
for this. You can think of it in the context of something like the game
Wurdle, where the user selects those scrabble-like tiles and the tiles each
have a predefined character associated with it.
Yes, the original suggestion to use strstr(). It is slow, for a word that's
never found it takes 0.704 seconds on the device. Definitely too long.
To answer your other questions...
It's initalized like this:
NSString *filePath = [[NSBundle mainBundle]
pathForResource: @"dictionary"
ofType: @"txt"];
stringFileContents = [[NSData alloc]
initWithContentsOfMappedFile:filePath];
On Thu, Apr 16, 2009 at 5:55 PM, Greg Guerin <email@hidden> wrote:
> Miles wrote:
>
> const char *fileBytes = [stringFileContents bytes];
>> char *ptr = strstr(fileBytes, cString);
>>
>
>
> Are you certain the bytes returned by [stringFileContents bytes]
> null-terminated? How was that data initialized? If it's reading from a
> file, is there guaranteed to be a null-terminator in the file? What happens
> if there isn't? Suppose the file gets edited and loses any overt
> null-terminator you have now, what will your program do?
>
> Was the original suggestion to use strstr(), or was there a similar
> function with a length?
>
> You also need to think about case-sensitivity, because searching for "joy",
> "Joy", "joY", or "JOY" might be different depending on where the search-term
> is coming from, and what letter-case means (or doesn't mean) in context.
>
>
> And what is the context? I don't think you ever said what you were trying
> to accomplish with this dictionary and searching in it.
>
> And since you have the words "options for speed" in the Subject, it's
> possible that strstr()'s linear search could be a speed problem. Or it's
> possible that linear search is fine (for worst-case, test it with a word
> that you know isn't anywhere in the dictionary). And that goes back to the
> "What are you trying to accomplish?" question, now that the "how to" has
> been more or less answered, at least for one case.
>
>
> -- GG
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden