Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

##Consistency of results

Consistency of results

##Consistency of results

Consistency of results

replaced http://programmers.stackexchange.com/ with https://softwareengineering.stackexchange.com/
Source Link
Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419

##Consistency of results

When you compile a Java program, you don't actually do much in the way of performance optimization of the code. Some basic things are done, but the real optimization is done by the JIT compiler at Java runtime.

The JIT compiler is able to take your class methods and analyze them, using statistics it gathers during your method rin,a n other items, to make your code really efficient. It can them mke bbigger decisions about performance as well. JIT is the performance king.

The JIT process takes time though, and the cost of the compile for code that runs once, or just a few times, is seldom worth it. So, JIT does nothing until the code has run many times (hundreds). Then, even if the code has been JIT compiled once, it may be determined that the code would benefit from a second, and even third compile with different characteristics.

It is only when you get to this final stage that it starts to make sense to performance-time/benchmark the code. The JIT based code can be thousands of times faster than the uncompiled code.

Consider this code here: http://programmers.stackexchange.com/a/246535/109836

The first 1000 loops ran at an average of > 7ms.... average.

The second 1000 loops ran at an average of 1.6ms

The 101st 1000 loops ran at an average of 1.2ms.

The code was well-compiled by then.

When benchmarking Java, you have to 'warm up' the code so that you are not running in to compile times, or poorly compiled code when you do your performance work. warming up normally requires running the sensitive code thousands of times (one of the best optimizations available is inlining method calls, and that requires the calling method to be run a lot, not just the actual code doing the work).

The median of three

The median of three logic is relatively simple, though it can be confusing.

If you have three values, A, B, and C. What are the possible combinations?

A B C
A C B
B A C
B C A
C A B
C B A

Now, if A is less than B, and B is less than C, let's follow the logic through:

if(arr[beginning] > arr[middle])
 swap(arr, beginning, middle);

Is the first bigger than the second value. This is possible with the following combinations (marked with an asterist *):

A B C
A C B
B A C *
B C A
C A B *
C B A *

So, half the combinations could be like this. If it is, then swap the first two:

A B C
A C B
A B C *
B C A
A C B *
B C A *

OK, so, now for the second test:

if(arr[beginning] > arr[end])
 swap(arr, beginning, end);

I'll mark the instances where this is possible with a hash mark #

A B C
A C B
A B C *
B C A #
A C B *
B C A * #

Swap them if they are:

A B C
A C B
A B C *
A C B #
A C B *
A C B * #

Final test is:

if(arr[middle] > arr[end])
 swap(arr, middle, end);
A B C
A C B @
A B C *
A C B # @
A C B * @
A C B * # @

Swap them:

A B C
A B C @
A B C *
A B C # @
A B C * @
A B C * # @
lang-java

AltStyle によって変換されたページ (->オリジナル) /