QUITERS NEVER WIN

0 Background

Get the index from input value of a container, like array/vetcor/etc…, is the basic feature every programmer needs to implement in daily work. When the volume is really low, no one cares about the performance, a correct output is all the requirement. If the volume is really high, facing million/billions of data stored in the container, the performance would become more and more important. We have to use all we have to speed up the scenario to get find the input value. In fact, the key is as simple as everyone knows, what is just reduce the loop count and use as much informations we have as we can. Let’s start with the very beginning.

1 Find the Index in an Array.

How to find the index from a input value in an array, or so other container? Yes, just loop the iterator though the container, until what we get is equal to the input, like this

Here we have T(n) = O(n), if the size is large enough, performance will be really poor.

2.Find the Index in an sorted Array

In our daily usage, lots of arrays are sorted. For example, a list of payload which constructs a file, every start offset are increase sorted of course. How to improve the performance? Yes, we have to use the information we got -‘sorted’, and dichotomy search.I think every programmer could figure it out and implement no matter how junior he is.We use Cpp array here as one example,

Here we have the terminate condition highlighted above, which means the ‘low’ iterator is just near the ‘high’ iterator, and the rest could not be ‘dichotomy’ any more. Since in Cpp, integer cast is the biggest integer small than the calculation result(divided by 2), there is chance that the input is just what ‘high’ iterator points.

In theory, T(n) = O(log n), worst case would be better than O(n), even the average performance n/2 above. Here we have ‘loop’ to indicate how many loops we take to get the output, so let’s just try an array with 16 elements to verify.

Finally, we could found the maximum loop count is only log(16) = 4 as above, which is just as our expect. Perfect, simple algorithm, but really useful~

3 Extension – Head, tail, as much hint as we could use

Now we have gone through this really simple and useful algorithm, Dichotomy Search, which could improve our search performance to such an extend, but sometimes the problem in life is much more interesting. Let’s just start from the requirement, such as the payloads within one file which was mentioned above.

Usually, we have one container to store the payload offset according to payload index, like offset[0] = 0, offset[1] = a, offset[2] = b, offset[3] = c, …, offset [n] = x, where x >c>b>a, but sometimes, since the free space between payload is 0, which means we could calculate the offset of one payload if we have the last payload offset and the last payload size, like

offset[n] = 0, n = 0

offset[n] = offset[n-1] + size[n-1] , n > 0

and some constructive payload has the same size, only the size and the payload count would be stored in the container to save the memory consumption, like

Yes, just loop the entry container, add the offset delta(payload size) of all the former payload unit, and then we could get the expected offset. Of course, T(n) = O(n), in which n is the entry counts, and if the input payload index is almost the last ones, we have to loop all the entries to calculate the output.

Could we use the dichotomy as bove? Absolutely not because offset[n] = offset[n-1] + size[n]. We have go through all the recursive ones to obtain the current value unless there is any general way related to ‘n’ when calculating the output.

Let’s adding one more condition to make this case more easier. This hole file is consisted of payloads and there are only payloads in the file, and the file size is easy to be obtained, which means,

offset[n] = 0, n = 1;

offset[n] = offset[n-1] + size[n-1], n > 1;

offset[n] = fileSize – size[n], n = payloadCount

Yes, here is one more information besides the ‘head’ one, which is ‘tail’ one. How could we use this to optimize our code? As the implementation above, the worst part is the later ones, because the input index is closer to the tail, more entries need to be loop when to help calculate the offset. If we could use ‘tail’ offset if the input index is ‘later’ one, the speed up could be reached, as below,

offset[n] = offset[n-1] + size[n-1], former ns

offset[n] = offset[n+1] – size[n], later ns

Since the entry’s value is really random, which means, ‘later half’ of the payloads has the expectations to exist in the ‘later half’ entries, closer to the ‘tail’ than the ‘head’, then it could be described as,

Here we have a indicator to help separate increase and decrease search case as highlighted.

4 Extension – Previous Results as hints

The ‘tail’ – file size – must be thanked to when the above implementation has been optimized finally. One more information got, then used, then succeed. Is there any opportunities to make it more faster? Of course, as long as we could have more useful information.

In our daily work, the algorithm/interface will not be used only once during one process life-cycle at most times, which means, when we use this function, probably it has already been called and the calculation procedure has already been executed. Yes, that’s true, we could just store the previous output for the trade-off for the performance by memory consumption. Here only the simplest case would be considered, which is, only use one previous output to help speed up our algorithm.

But how? Just like how we use ‘tail’ information! The closer iterator starter, the faster. It could be explained as below,

Take the code above as reference, I think this is not very hard to implement. This only take one previous output as hint. If the performance is highly required, we could consider take every previous output to speed up, and it is just the same. There is one more thing needed to be paid attention on, which is if there is more previous outputs than entry count, a lot of time would be needed to calculate every ‘mid’ and compare to decided which one should be the starter when loop all the previous outputs. This is something like ‘sampling’, and is not in this article’s scope.

The performance improvement is decreasing when the previous output hint count is increase, just be careful~