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