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)
However another very common pattern is to write code like this:
1: if (!memcmp(a, b))
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.