test/jdk/java/util/TimSort/SortPerf.java
author dl
Tue, 16 Jan 2018 18:28:39 -0800
changeset 48541 946e34c2dec9
parent 47216 71c04702a3d5
permissions -rw-r--r--
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01 Reviewed-by: martin
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
public class SortPerf {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    25
    private static final int NUM_SETS = 5;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    26
    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
    27
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    28
    // 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
    29
    private static int reps(int n) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    30
        return (int) (12000000 / (n * Math.log10(n)));
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    31
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    32
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    33
    public static void main(String[] args) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    34
        Sorter.warmup();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    35
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    36
        System.out.print("Strategy,Length");
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    37
        for (Sorter sorter : Sorter.values())
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    38
            System.out.print("," + sorter);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    39
        System.out.println();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    40
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    41
        for (ArrayBuilder ab : ArrayBuilder.values()) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    42
            for (int n : lengths) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    43
                System.out.printf("%s,%d", ab, n);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    44
                int reps = reps(n);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    45
                Object[] proto = ab.build(n);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    46
                for (Sorter sorter : Sorter.values()) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    47
                    double minTime = Double.POSITIVE_INFINITY;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    48
                    for (int set = 0; set < NUM_SETS; set++) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    49
                        long startTime = System.nanoTime();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    50
                        for (int k = 0; k < reps; k++) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    51
                            Object[] a = proto.clone();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    52
                            sorter.sort(a);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    53
                        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    54
                        long endTime = System.nanoTime();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    55
                        double time = (endTime - startTime) / (1000000. * reps);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    56
                        minTime = Math.min(minTime, time);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    57
                    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    58
                    System.out.printf(",%5f", minTime);
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.println();
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
        }
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
}