|
|
|
|
|
by jokabrink
1342 days ago
|
|
Is this even comparable? I mean, for C you have a three-way comparison and for C++ there is only a two-way comparison. If you run the code by yourself you end up that C (while calling the compare() less often than C++) needs more comparisons (i.e. more '<' and '==') kinda defeating the argument of costly comparisons: N = 2**16:
qsort comparison calls 14.735568
qsort comparisons 22.103386
std::sort comparisons 19.294233
|
|
Edit: For primitives we can also consider what happens in the processor. A comparison consists of two parts. First loading the operands and comparing them (loading is the expensive part here), and then a conditional branch instruction. A three-way comparison only loads the operands once and so has the same memory pressure but it does put more stress on the branch predictor. Not quite twice though, because the second branch instruction will only be encountered about half the time, and it will usually take the same path as elements are usually not equal.