开发者

Java 7 sorting "optimisation"

In Java6 both quicksort and mergesort were used in Arrays#sort, for primitive and object arrays respectively. In Java7 these have b开发者_StackOverflowoth 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.


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.


I wrote 2 methods test1 and test2 and add the main part of the compiled byte code (Java 1.6 on Snow Leopard) as comment:

    /*
     *     14  iload_1 [b]      -> load value from address 1 to sack
     *     15  iastore          -> store value from stack into int array
     *     16  iinc 3 1 [i]     -> int increment value of address 3
     *     19  iinc 3 1 [i]     -> int increment value of address 3
     */
    public void test1() {
        int b = 0;
        int a[] = new int[10];
        for (int i=0; i<10; i++) {
            a[i] = b; 
            i++;
        }
    }

    /*
     *     14  iinc 3 1 [i]     -> increment value of address 3
     *     17  iload_1 [b]      -> load value from address 1 to stack
     *     18  iastore          -> store value from stack into int array
     *     19  iinc 3 1 [i]     -> increment value of address 3 
     */
    public void test2() {
        int b = 0;
        int a[] = new int[10];
        for (int i=0; i<10; i++) {
            a[i++] = b;
        }
    }

The order of the inc ops is different. But the operation sum of both methods test1 and test2 are equal! So the byte codes performance should be the same, too.


There is a way that allows you to see the processor instructions generated by the hotspot engine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜