Search code examples
javabinaryjava-7java-6

Performance hit with binary literals in Java 6 as compared to Java 7


I am reading a book , it says

Before Java 7 , if you want to convert a binary literal in Integer.

you need to write

int x = Integer.parseInt("1100110", 2);

but after Java 7

int x = 0b1100110;

Java6 practise gives

1) There is a performance hit for that method call. -> JVM does inline expansion and other optimizations also .

2) It's really verbose. -> what this point means ?

3) It makes life hard for JIT. -> how ? I don't see any reason

Sorry but i didn't get , what these three points means ? Can someone please explain or redirect me to some link where i can know about it ?


Solution

    1. When you call Integer.parse, the conversion of binary string representation is done at runtime, so some CPU cycles are spent to do it; When you use a binary constant, the conversion is done at compile time, so there is no performance hit.
    2. Method call takes more characters to write than the 0b prefix, so it is more verbose.
    3. JIT will have harder time figuring out the value of x when you use parse, so it wouldn't be able to mitigate the first issue, at least not easily.

    It goes without saying that all of these points are about a micro-optimization, and that it is hard to imagine a situation when it would make a palpable difference.