2 ms·
Quick take, assuming equal length of a and b: For each byte index i: x[i] = a[i] XOR b[i] return sum(x) == 0
by continuational 3y ago
Quick take, assuming equal length of a and b:
For each byte index i:
x[i] = a[i] XOR b[i]
return sum(x) == 0
- sjducb 3y agoI think that’s still affected by speculative execution. The CPU will process the step ahead assuming that a[i] != b[i] while it is doing the a[i] XOR b[i] check. So it’ll be faster if: most characters match, most characters don’t match or there are runs of matching characters. Have a read of this: https://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-processing-an-unsorted-array https://stackoverflow.com/questions/11227809/why-is-processi...
- Ar-Curunir 3y agoThere is no branch here, so nothing for the CPU to speculate on.
- bluGill 3y agoThere is no obvious branch, but the compiler is allowed to implement the as-if rule and insert a branch if it figures out what you are doing.
- faceplanted 3y agoIn theory that would work but compilers and toolchains are far too clever now and always liable to change, so there's no way to know that in the future it won't be optimised back into shortcutting and revealing the timings.