This generates a for loop assembly in main() when the are_equal_manual(left,right) function is used:
.L21: xor esi, esi test eax, eax jne .L20 cmp edx, 2 sete sil .L20: mov rax, rcx movzx esi, sil mul r8 shr rdx, 3 lea rax, [rdx+rdx*4] mov edx, ecx add rax, rax sub edx, eax mov eax, edx mov edx, ecx add rcx, 1 and edx, 7 cmp rcx, rdi
And this is what is generated when using the are_equal_alg(left,right) function:
.L20: lea rsi, [rsp+16] mov edx, 16 mov rdi, rsp call memcmp mov ecx, eax mov rax, rbx mov rdi, rbx mul r12 shr rdx, 3 lea rax, [rdx+rdx*4] add rax, rax sub rdi, rax mov eax, ebx add rbx, 1 and eax, 7 cmp rbx, rbp mov DWORD PTR [rsp], edi mov DWORD PTR [rsp+24], eax jne .L20
I'm not quite sure what is going on in the generated code for the first case, but explicitly does not call memcmp() . It doesn't seem to compare the contents of arrays at all. Although the loop still repeats 5,000,000,000 times, it is optimized to do nothing. However, the loop that uses are_equal_alg(left,right) still does the comparison. Basically, the compiler is still able to optimize the comparison manually much better than the std::equal pattern.