Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 7 sorting "optimisation"

Tags:

In Java6 both quicksort and mergesort were used in Arrays#sort, for primitive and object arrays respectively. In Java7 these have both changed, to DualPivotQuicksort and Timsort.

In the source for the new quicksort, the following comment appears in a couple of places (eg line 354):

 /*   * Here and below we use "a[i] = b; i++;" instead   * of "a[i++] = b;" due to performance issue.   */ 

How is this a performance issue? Won't the compiler will reduce these to the same thing?

More broadly, what is a good strategy for investigating this myself? I can run benchmarks, but I'd be more interested in analysing any differences in the compiled code. However, I don't know what tools to use etc.

like image 362
Matthew Gilliard Avatar asked Jul 13 '11 09:07

Matthew Gilliard


1 Answers

This is only an answer to the general question.

You can look at the bytecode and try to understand the differences. I.e. you could write yourself a simple example using both a[i] = b; i++; and a[i++] = b; and see whats the difference.

The simplest way to show the bytecode is the javap program (should be included in you JDK). Compile the code with javac SomeFile.java and run javap on the code: javap -c SomeFile (the -c switch tells javap to output the bytecode for each method in the file).

If you are using eclipse you could also try this one.

like image 93
Martin Thurau Avatar answered Sep 18 '22 05:09

Martin Thurau