Why is memcmp so much faster than a for loop check?

后端 未结 3 670
温柔的废话
温柔的废话 2020-11-28 12:03

Why is memcmp(a, b, size) so much faster than:

for(i = 0; i < nelements; i++) {
    if a[i] != b[i] return 0;
}
return 1;

I

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 12:29

    It's usually a compiler intrinsic that is translated into fast assembly with specialized instructions for comparing blocks of memory.

    intrinsic memcmp

提交回复
热议问题