如何在一个步骤中测量排序算法的时间?

时间:2014-07-19 20:19:51

标签: java arrays algorithm sorting

我正在进行一项练习,但我仍然坚持一点

我想使用七种不同的数组来测量3种排序算法(气泡,插入和选择)的排序时间。我尝试了多种方法,但我无法在一个步骤中为所有阵列测量所有三种算法的排序时间。我的计划应该:

  • 用整数随机填充数组
  • 对它们进行排序
  • 衡量时间
  • 打印时间

结果总是在0毫秒之内,但它不能是那样的,因为我尝试了100万个整数数组,所以不可能为它提供0毫秒。最后,我尝试了#34; for循环"为了实现我的目标。

我应该编写所有方法,因为您可能会在其他方法中发现其他错误。

public static void randomlyFillArray(int[] array, int a, int b) {
    for (int i = 0; i < array.length; i++) {
        array[i] = randomInt(a, b);
    }
}

public static int randomInt(int a, int b) {
    return (int) ((b - a + 1) * Math.random() + a);

}

public static void SelectionSort(int[] array) {

    for (int i = 0; i < array.length - 1; i++) {
        for (int j = i + 1; j < array.length; j++) {
            if (array[i] > array[j]) {
                // ... Exchange elements
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
}

public static void insertionSort(int[] array) {
    int i, j, temp;
    for (i = 1; i < array.length; i++) {
        temp = array[i];
        j = i;
        while (j > 0 && array[j - 1] > temp) {
            array[j] = array[j - 1];
            j--;
        }
        array[j] = temp;
    }
}

public static void bubbleSort(int[] array) {
    boolean swapped = true;
    int j = 0;
    int temp;
    while (swapped) {
        swapped = false;
        j++;
        for (int i = 0; i < array.length - j; i++) {
            if (array[i] > array[i + 1]) {
                temp = array[i];
                array[i] = array[i + 1];
                array[i + 1] = temp;
                swapped = true;
            }
        }
    }
}

public static void main(String[] args) {
    // int[] array500 = new int[500];  //These are my arrays that I should do the process for //each one.
    // int[] array1000 = new int[1000];
    // int[] array5000 = new int[5000];
    // int[] array10000 = new int[10000];
    // int[] array50000 = new int[50000];
    // int[] array100000 = new int[100000];
    // int[] array500000 = new int[500000];
    // int[] array1000000 = new int[1000000];
    for (int i = 0; i < 4; i++) {

        int[] array = new int[500];
        if (i == 1) {
            randomlyFillArray(array, 1, 1000);
            SelectionSort(array);

            long startTime = System.currentTimeMillis();
            long total = 0;

            long stopTime = System.currentTimeMillis();
            long elapsedTime = stopTime - startTime;
            SelectionSort(array);
            System.out.println("SelectionSort for 500 integer :  "
                    + elapsedTime);

        } else if (i == 2) {
            randomlyFillArray(array, 1, 1000);
            insertionSort(array);
            long startTime = System.currentTimeMillis();

            long total = 0;

            long stopTime = System.currentTimeMillis();
            long elapsedTime = stopTime - startTime;
            insertionSort(array);
            System.out.println("InsertionSort for 500 integer :  "
                    + elapsedTime);
        } else if (i == 3) {
            randomlyFillArray(array, 1, 1000);
            bubbleSort(array);

            long startTime = System.currentTimeMillis();
            long total = 0;

            long stopTime = System.currentTimeMillis();
            long elapsedTime = stopTime - startTime;
            bubbleSort(array);

            System.out.println("BubbleSort for 500 integer :  "
                    + elapsedTime);

        }

    }

}

感谢您的所有建议stackoverflow系列。

此致。

2 个答案:

答案 0 :(得分:4)

所有时间块都是这样的:

randomlyFillArray(array, 1, 1000);
SelectionSort(array);
long startTime = System.currentTimeMillis();
long total = 0;
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
SelectionSort(array);
System.out.println("SelectionSort for 500 integer :  " + elapsedTime);

您排序,然后占用开始时间,然后立即采用结束时间,然后再次排序,然后打印时间。您必须在排序后使用结束时间。如果您有两次排序的原因('预热'JVM?),请确保在进行定时排序之前重新随机化阵列。用于对已经排序的数组进行排序的算法性能可能大不相同。

randomlyFillArray(array, 1, 1000);
long startTime = System.currentTimeMillis();
long total = 0;  // this thing is never used...
SelectionSort(array); // move this line between start time and end time!
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("SelectionSort for 500 integer :  " + elapsedTime);

此外,对于每种类型的代码,大部分代码都是相同的。您可以将这些部分移出if/else块并进入循环,从而使整个代码更紧凑,更易于维护。此外,您可以围绕不同的数组大小创建另一个循环。

for (int num : new int[] {500, 1000, 5000, ...}) {
    for (int i = 0; i < 4; i++) {
        String sort = null;
        int[] array = new int[num];
        randomlyFillArray(array, 1, 1000);
        long startTime = System.currentTimeMillis();
        if (i == 1) {
            sort = "SelectionSort";
            SelectionSort(array);
        } else if (i == ...) {
            // analogeous for other sorts
        }
        long stopTime = System.currentTimeMillis();
        long elapsedTime = stopTime - startTime;
        System.out.println(sort + " for " + num + " integers: " + elapsedTime);
    }
}

答案 1 :(得分:2)

创建微基准测试可能很棘手,因为JIT编译了经常使用的代码,这需要一些时间...

所以我的方法通常是这样的:

public void measureSorting(int[] array) {
    // this should be sufficiently large
    long start, end;
    randomlyFillArray(array, 1, 1000);

    // warm up JIT - execute without measuring
    SelectionSort(array);
    randomlyFillArray(array, 1, 1000);

    // stop the time
    start = System.currentTimeMillis();
    SelectionSort(array);
    end = System.currentTimeMillis();

    System.out.printf("SelectionSort for %d integer :  %d milliseconds", array.length, end-start);

    // repeat for other algorithms
}

如果您使用的是Java8,您甚至可以创建如下内容:

public static void measureSorting(int[] array, Consumer<int[]> sortingFunction) {
    // this should be sufficiently large
    long start, end;
    randomlyFillArray(array, 1, 1000);

    // warm up JIT - execute without measuring
    sortingFunction.accept(array);
    randomlyFillArray(array, 1, 1000);

    // stop the time
    start = System.currentTimeMillis();
    sortingFunction.accept(array);
    end = System.currentTimeMillis();

    // repeat for other algorithms

    System.out.printf("%s for %d integer :  %d milliseconds", sortingFunction, array.length, end-start);
}

public static void main(String[] args) {
    // assuming the enclosing class is Sorter
    measureSorting(new int[500], Sorter::SelectionSort);
    measureSorting(new int[500], Sorter::insertionSort);
    measureSorting(new int[500], Sorter::bubbleSort);
}