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

后端 未结 3 658
温柔的废话
温柔的废话 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:31

    Is memcmp a CPU instruction or something?

    It is at least a very highly optimized compiler-provided intrinsic function. Possibly a single machine instruction, or two, depending on the platform, which you haven't specified.

提交回复
热议问题