c - Array compare with memcmp vs element by element comparsion -
this question has answer here:
- why memcmp faster loop check? 4 answers
which implementation better , why?
element element comparison using loop or memcmp() implementation
int a[] = {1,2,3}; int b[] = {1,3,5}; memcmp(a, b, sizeof(int)*n)
or
(i = 0; < n ; i++) { if (a[i] == b[i]) { /* code */ } }
memcmp
faster: heavily optimized compiler use special instructions on cpu, loop unrolling, , other "advanced" techniques generated code simple loop (generally) won't use. however, memcmp
can compare byte values; fine in case of array of integers, (probably) wouldn't work array of objects.
Comments
Post a Comment