3 ms·
I'm a bit confused... why would that function be faster than `return key1 != key2` ? Not sure how to compile this function to a 16b target, otherwise I'd check
by viraptor 15y ago
I'm a bit confused... why would that function be faster than `return key1 != key2` ?
Not sure how to compile this function to a 16b target, otherwise I'd check myself :(
- lukesandberg 15y agoI think the point is that he needed a comparison function not and equals function. thus the subtraction. Erric Lippert had a nice series on subtle bugs in compareTo implementations, in part3 he talks about a similar version of this bug. http://blogs.msdn.com/b/ericlippert/archive/2011/01/27/spot-the-defect-bad-comparisons-part-three.aspx http://blogs.msdn.com/b/ericlippert/archive/2011/01/27/spot-... tldr; if you aren't doing a comparison in your compareTo implementation, you probably have a bug.
- viraptor 15y agoI thought it might too, but then he writes: "A zero result means equals, and non-zero means not-equals… and then the results are cast to 32 bits." So he didn't care about ordering.
- andrewf 15y agoTypically, at the assembly level, doing a "compare" will change the contents of some CPU flags, but not registers. Normally, those CPU flags would then influence future instructions - most often a "GOTO if this flag is set" instruction. In this case, you want to return the result of the comparison from a function. Since functions return values in registers, you'd need to do some extra work to copy the result into a register from the flag. Between compiler optimisations, and the cost of your function call in the grand scheme of things, the difference is likely negligible. This may, or may not, have been the case in 1991. Also, casting to an integer pointer rather than a char pointer means the code has to divide by 4 at least once. (He did write this "optimisation" as an undergraduate!) EDIT: Actual assembly comparison, from gcc -O3 (Apple's 4.2.x). subl (subtract) and cmpl (compare) take the same amount of time. But then _compare has to do setne to to store the "not equal" flag into a register, then movzbl to expand that result from 8 bits (which is what setne writes) to the full size of an int. _subtract: // return (int)((char*)key1 - (char*)key2)) pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax subl 12(%ebp), %eax leave ret _compare: // return key1 != key2 pushl %ebp movl %esp, %ebp movl 12(%ebp), %eax cmpl %eax, 8(%ebp) setne %al movzbl %al, %eax leave ret
- viraptor 15y agoStrange, new gcc (4.4.4) produces a much nicer assembly for key1!=key2: .LFB0: .cfi_startproc xorl %eax, %eax cmpq %rsi, %rdi setne %al ret .cfi_endproc Clang 2.8 does something similar to your _compare (I expect new versions to be a bit better)
- andrewf 15y agoDifferent calling conventions. The calling convention for my target (32-bit x86 Mac OS X) says that all arguments have to be passed in on the stack. For your 64-bit target, the arguments go into registers.
- ori_b 15y agoYou're comparing two different assembly languages and calling conventions. x64 != x86.
- viraptor 15y agoYou're right. It's been some time since I've looked at any intel-like assembly :)