I want to get the most speed-optimal code out of this frequently run comparison:
int Object::Compare(Object *other) {
if(id < other->id) return -1;
if(id > other->id) return 1;
return 0;
}
The id
var is an unsigned 32-bit integer.
What I'm finding is that even with speed optimizations turned on via a pragma (ordinarily the debug build doesn't use optimizations), I'm still seeing awful inefficiency in the assembly where it's reading the id vars twice and comparing twice, even though I only need the comparison to be done once.
Variations on this code have not helped. In this specific case I'm compiling in Visual C++ and looking at the output, although the code is meant to also compile for Linux environments (still x86 though). What I'm wondering is, is there a better way to structure this so the compiler can understand it should only compare once and then use comparison flags to handle the rest?
Even better would be if I can get any non-branching instructions like cmov
in play. Any thoughts?