c - Most efficient search on small array? -


I have an array where it rarely (almost never) gets updated:

< Pre> unsigned long user_values ​​[8];

I want to be able to create a lot of quick ups (hundreds of times per second on a slow machine), to check that the value is in the array and if it is, its index Find almost all the time, it will fail to find items in the lookup array.

Normally, I keep the array sorted, and use binary search, but I am unsure of the efficiency of binary, search on a very small data set. Looking at this little known shape, are there any more efficient ways to see it?

I give some suggestions such that most of the failures are immediately detected by a small hash . Something like this:

  #define HASH (x) ((x) & amp; 0xff) // if possible, uint8_t is possible [256]; Zero initHash () {int i; Memory (possible, 0, size (potential)); For (i = 0; i <8; i ++) possible [HASH (user_values ​​[i])] = 1; } Int search (unsigned long wal) {// Most Failure Rule with a Quick Test If (possible! [Hush (val)]) -1 returned; // Now use either binary or linear search ...}  

Note that with 25 slot hash tables set in 8 slots, you immediately get 31/32 or 97% failures Will end. There are three clear ways to improve it:

  1. You can make the hash table larger for filtering more failures, but at the cost of using more memory. Better yet, you can add a second hash (for example, the hash another on the previous byte) it will only take another 256 bytes but will align 97% of the 3% that passed the hash, this algorithm of several swans Is called the Bloom Filters.
  2. You can use 1-bit per hash index instead of a byte which makes the hash table 1/8 size, but for this calculation of length is required to check the hash.
  3. Depending on what kind of data you are expecting, you can provide a better hash function.

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -