Squeezing performance out of memcmp usage

When I wrote this the other day it made me think of another thing involving the memcmp function and the VC compiler. In the code I’ve seen over the years where memcmp was used it was always to find out if an area was identical or not. So the code typically looked something like this:

1: if (memcmp(a, b) == 0)
2: doSomething(a);
3: else
4: doSomething(b);

However another very common pattern is to write code like this:

1: if (!memcmp(a, b))
2: doSomething(b);
3: else
4: doSomething(a);

Turns out the latter results in faster code when compiled (at least with the VC compiler)! How is that? Turns out the compiler recognizes that you’re not really interested in if memory block a is lower or greater than block b and the key to do this is the not operator. So by using the not operator the compare operation does not need to find the first difference and calculate which block is less than the other. The compiler instead generates code that just checks if the memory blocks are equal or not, which is much faster.

Comments (1)

  1. My understanding is that this micro-optimisation can also be used when you also want to know a and b are different:

    if (!!memcmp(a, b))

    is faster than

    if (memcmp(a, b))