为什么memcmp(a, b, size)比这快得多:
for(i = 0; i < nelements; i++) {
if a[i] != b[i] return 0;
}
return 1;
Run Code Online (Sandbox Code Playgroud)
memcmp是CPU指令还是什么?它必须非常深,因为我memcmp在循环中使用了大量的加速.
我不确定下面的代码有多快.如果有人知道比这更快/更优化的代码,请告诉我.
int xstrcmp(char *s1, char *s2)
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(const unsigned char *)s1 - *(const unsigned char *)(s2-1));
}
Run Code Online (Sandbox Code Playgroud)