samedi 20 août 2016

How much "if" statements effect on performance?

There are a some IPTables with different sizes (e.g 255 or 16384 or 512000!!).Every entry of each table, holds a unique IP Address (hex format) and some other values. The total number of IPs is 8 millions. All IPs of all IPTables are sorted

We need to search IPTable 300,000 times per sec. Our current Algorithm for finding an IP is as follow:

// 10 <number of IPTables <20
//_rangeCount = number of IPTables 
s_EntryItem* searchIPTable(const uint32_t & ip) {
        for (int i = 0; i < _rangeCount; i++) {
            if (ip > _ipTable[i].start && ip < _ipTable[i].end) {
                int index = ip - _ipTable[i].start;
                    return (_ipTable[i].p_entry + index);
                }
            }
            return NULL;
        }

As it can be seen, in worst case, number of comparisons for a given IP address is _rangeCount *2 and number of "if" statement checking is _rangeCount.

Suppose i want to change the searchIPTable and use more efficient way to find an IP address in IPTables. as far as i know, for a sorted array, the best software implementation of a famous search algorithm like binary search needs log(n) comparisons( in worst case).

So, the number of comparisons to find an IP address is log(8000000) that is equal to ~29000.

Question 1:

As it can bee seen there is a huge gap between the number of comparison needed by two algorithm ( _rangeCount vs 29000) but in first method, there are some "if" statement that could effect on performance. if you want to run first algorithm for 10 times, obviously the first algorithm has better performance, but i have know idea about running two algorithm for 3000,000 times! what is your idea?

Question 2:

Is there a more efficient algorithm or solution to search IPs?

Aucun commentaire:

Enregistrer un commentaire