3 ms·
Ah I see the problem. Actually I used a Ruby console. So what then is the CORRECT way of doing this comparison in C, avoiding potential overflow pitfalls?
by seivadmas 12y ago
Ah I see the problem. Actually I used a Ruby console.
So what then is the CORRECT way of doing this comparison in C, avoiding potential overflow pitfalls?
- cygx 12y agoreturn (x > y) - (x < y);
- vardump 12y agoThat can be very expensive operation. Potentially two branches, not counting return from subroutine.
- cremno 12y agoIf performance really is of concern, qsort() or similar functions probably shouldn't be used. Instead a dedicated function, which allows to choose a specific algorithm (qsort() doesn't have to use quicksort) and also doesn't involve calling a comparison function pointed to by a function pointer, can be used.
- bluecalm 12y agoOne interesting thing I have learned recently is that GCC can do inlining through function pointers. That doesn't make your point about dedicated function being better idea for performance but it's one thing "std::sort is faster by design" people often miss. From GCC documentation: >>-findirect-inlining Inline also indirect calls that are discovered to be known at compile time thanks to previous inlining. This option has any effect only when inlining itself is turned on by the -finline-functions or -finline-small-functions options. Enabled at level -O2.
- cremno 12y agoBut GCC likely isn't able to do that for libc functions like qsort(). Maybe if LTO is enabled and libc is linked statically, it might.
- bluecalm 12y agoI have no idea when it can and when it can't do that to be honest. I tested qsort vs std::sort on my machine on my data and performance was the same (Windows, MinGW, GCC 4.8, -flto enabled) but other people reported different results.
- azakai 12y agoI would hope that on modern compilers on modern architectures, this would be done with zero branches. That is the case in a quick test on my machine (gcc 4.6.3).
- vardump 12y agoGood if compilers are that smart nowadays. Just a few years ago I did see two branches in a very similar piece of code.
- gonzo 12y agoNot really. Maybe on an old atom.
- userbinator 12y agoThe simplest way to implement that would be two SETxx instructions and a subtract on 386+, SLT/SGT on MIPS, two subtracts (or one compare and one subtract) and two predicated moves on ARM. No branching required at all.
- Too 12y agoThe correct way to do a comparison is to use a comparison operator...... If x< y etc...