Microbenchmarks like this don't tell you anything interesting. You are doing a billion iterations of the test and the result comes back in seconds. How many cycles per iteration is that? The microbenchmark isn't doing any work.
The differences between !, != and == look like random noise - were you really expecting them to come out to exactly the same number of millisecs?
The improvement with JVM version, however, is almost certainly real, though it is likely very specific to that particular piece of code, a matter of something falling within a complexity threshold to be handled properly. Something even slightly different might not show the same results.
To improve testing, calculate the standard deviation of each test run and see if they are statistically different (or just print out all 10 results and eyeball them).
If the JVM is doing a decent job it will detect that the following statements do nothing that affects the computation and will completely optimize them away.
if (truFalse != true) {
//do nothing...
}
...
if (truFalse == false) {
//do nothing...
}
...
if (!truFalse) {
//do nothing...
}
In other words, your benchmarks are probably not measuring anything different in the three cases.
Lessons to learn:
- It is difficult to be sure that you are getting meaningful numbers from a microbenchmark.
- The relative numbers can vary significantly from one JVM to another. A "clever trick" that helps on one JVM could actually get in the way on another.
- A compiler is likely to do a far better job of micro-optimizing your Java programs for each platform than you are.
The best strategy is to leave micro-optimization to the compiler, and focus on the "macro" issues like using the best algorithm. Also, use a execution profiler to figure out where it is worthwhile spending time to optimize.