I have a list of hundreds of unique strings in C ++, I have to check that a value exists in this list Or not, but authentically power fast.
I am using a hash_set with std :: string (because I can not find it to work with CONST * *) like:
Stdext: hash_set & lt; Const std :: string & gt; _items; _items.insert ("LONG_NAME_A_WITH_SOMETHING"); _items.insert ("LONG_NAME_A_WITH_SOMETHING_ELSE"); _items.insert ("SHORTER_NAME"); _items.insert ("SHORTER_NAME_SPECIAL"); Stdext :: hash_set & lt; Const std :: string & gt; :: const_iterator it = _items.find ("SHORTER_NAME")); If (this! = _items.end ()) {std :: cout & lt; & Lt; "Item exists" & lt; & Lt; Std :: endl; }
Is it a good idea for a fast search method to make someone else have a complete hashtable?
A fixed list of list strings will not change It contains a list of names of elements that are affected by a certain bug and should be repaired on the fly when opened with a new version.
I have created the hashtables before using the ego-corasik, but I am not ready to add a lot of complexity.
I was surprised by the number of answers I finished testing some methods for their performance and ended using a combination of Kirkus and Rob K.. I had tried a binary search before, but I think I could apply it to a small bug (how difficult it might be ...).
The result is shocking ... I thought that my use is faster than a hash_set ... OK, it ends, I did not. Here are some statistics (and last code):
Random lookup of 5 existing keys and 1 non-existent key, 50,000 times
My basic algorithm took 18,62 seconds - on average, a linear search took 2,49 seconds - a binary search on average 0, 92 seconds
Search took average 0,51 seconds using a right hattable generated by jeeper.
Here's the code I use:
bool searchWithBinaryLookup (const std :: string and strkey) {static const char arr arrays [] [ NUM_ITEMS] = {/ * Item list *}; / * Binary lookup * / int low, middle, high; Less = 0; High = NUM_ITEMS; While (low & lt; elevated) {medium = (low + high) / 2; If (arrAffectedSymbols [Middle]> strkey) {High = Middle 1; } And if (arrAffectedSymbols [Middle]
Note: This is Microsoft VC ++, so I am not using std :: hash_set from SGi.
I suggested some tests this morning as the gperf was used and it is actually a little faster.
If your list is compiled at the time of compiling wire, use gperf quote: gperf one The complete hash function generator is for the given list of stars, it produces a hash function and hash table in the form of C or C ++ code to see the value based on the input string. The hash function is correct, which means there is no collision in the hash table, and hash table lookup only requires a string comparison.
The output of gperf is not controlled by gpl or lgpl, afaik.
Comments
Post a Comment