Am 11.10.2011 19:49, schrieb Xueming Shen:

I don't know which one is better, I did a run on

    private static boolean op1(int b) {
        return (b >> 6) != -2;
    }
    private static boolean op2(int b) {
        return (b & 0xc0) != 0x80;
    }
    private static boolean op3(byte b) {
         return b >= (byte)0xc0;
    }

with 1000000 iteration on my linux machine,  and got the scores

op1=1149
op2=1147
op3=1146

I would interpret it as they are identical.
Me too. thanks for your effort.
Maybe the comparison would differ on different architectures.

So I would prefer opt3, because the others ...
1. in question need 1 more CPU register to save the original value of b for 
later usage
2. need 1 more constant to load into CPU
and opt 3 ...
3. is the best readable source code
4. in question seems best to help Hotspot finding best optimization on 
arbitrary architectures.

Additionally I guess using always byte variables would in question help HotSpot to optimize with 1-byte-operand CPU instructions.

Don't you like to replace all "(bx & 0xc0) != 0x80" by "isNotContinuation(bx)" ?

-Ulf

Reply via email to