Thank you for the comments.
@ptrblck Each iteration of the loop performs the comparison for two elements (that’s why the nested if). So in the end each element is only compared once, and the theoretical analysis is sound I think.
I’m testing the code with torch.set_num_threads(1). So I’m assuming  both algorithms are playing in level ground in terms of parallelization(?)
@googlebot Do you anticipate that if I disabled AVX2 (or tried the code on a CPU without AVX instructions), then the optimized code would runs faster?