I am not sure how fast the below code is. If anyone knows the faster/optimized code than this, please let me know.
int xstrcmp(char *s1, char *s2)
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(const unsigned char *)s1 - *(const unsigned char *)(s2-1));
}
Use
::strcmpinstead of your own hand-rolled version. Your compiler vendor has most likely an assembly-only version which uses CPU-specific features for comparison (SSE4.2 for instance has special instructions for fast string comparison.) The MSVC version is written in assembly for instance and uses larger compares (whole words instead of individual characters) as much as possible, special casing unaligned starts/ends of the string (if you have VS2010 installed, it’s inVC/crt/src/intel/strcmp.asm.)