faster memory compare for equality 16 bytes block than memcmp

1.8k views Asked by At

I have to compare for equality 16 bytes memory blocks in a very performance sensitive place.

The blocks are always perfectly aligned and they are always exactly 16 bytes. It seems to me that I should be able to utilize this knowledge and come up with something that works better than byte-by-byte comparison.

In fact I believe that most of the memcmp implementations do this, but obviously it will cost for it some time to analyze the addresses and the size. In my case it does not, and yet it is still faster:

...
mov    $0x10,%ecx
mov    -0x4c(%ebp),%esi
repz cmpsb %es:(%edi),%ds:(%esi)

I tried to optimize it with implementing 32 bits checks my self, but it does not perform better. Probably because memcmp utilize processor instructions, that my custom c++ code does not.

Any ideas is there something faster then memcmp for such a case?

1

There are 1 answers

3
Dmitry On

You can try something like this, just to see what difference does it will make comparing with memcmp (assuming, you have a 64bit processor):

#define MY_CMP(B1, B2) (((int64_t *) (B1))[0] == (int64_t *) (B2))[0] && ((int64_t *) (B1))[1] == ((int64_t *) (B2))[1])

if (MY_CMP(array1, array2)) {
    // something
}

But if comilator is good, you shouldn't see any difference.