jdk/test/java/util/TimSort/SortPerf.java
author yhuang
Wed, 17 Apr 2013 01:04:45 -0700
changeset 16908 c8a0f2183fed
parent 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     1
/*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     2
 * Copyright 2009 Google Inc.  All Rights Reserved.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     4
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     8
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    13
 * accompanied this code).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    14
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
    21
 * questions.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    22
 */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    23
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    24
import java.util.Arrays;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    25
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    26
public class SortPerf {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    27
    private static final int NUM_SETS = 5;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    28
    private static final int[] lengths = { 10, 100, 1000, 10000, 1000000 };
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    29
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    30
    // Returns the number of repetitions as a function of the list length
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    31
    private static int reps(int n) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    32
        return (int) (12000000 / (n * Math.log10(n)));
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    33
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    34
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    35
    public static void main(String[] args) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    36
        Sorter.warmup();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    37
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    38
        System.out.print("Strategy,Length");
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    39
        for (Sorter sorter : Sorter.values())
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    40
            System.out.print("," + sorter);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    41
        System.out.println();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    42
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    43
        for (ArrayBuilder ab : ArrayBuilder.values()) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    44
            for (int n : lengths) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    45
                System.out.printf("%s,%d", ab, n);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    46
                int reps = reps(n);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    47
                Object[] proto = ab.build(n);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    48
                for (Sorter sorter : Sorter.values()) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    49
                    double minTime = Double.POSITIVE_INFINITY;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    50
                    for (int set = 0; set < NUM_SETS; set++) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    51
                        long startTime = System.nanoTime();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    52
                        for (int k = 0; k < reps; k++) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    53
                            Object[] a = proto.clone();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    54
                            sorter.sort(a);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    55
                        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    56
                        long endTime = System.nanoTime();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    57
                        double time = (endTime - startTime) / (1000000. * reps);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    58
                        minTime = Math.min(minTime, time);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    59
                    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    60
                    System.out.printf(",%5f", minTime);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    61
                }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    62
                System.out.println();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    63
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    64
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    65
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    66
}