Java 如何在一个步骤中测量排序算法的时间?
我正在做一个练习,但有一点我被卡住了 我想用七种不同的数组测量3种排序算法(冒泡、插入和选择)的排序时间。我尝试了多种方法,但我无法在一个步骤中测量所有阵列的所有三种算法的排序时间。我的方案应:Java 如何在一个步骤中测量排序算法的时间?,java,arrays,algorithm,sorting,Java,Arrays,Algorithm,Sorting,我正在做一个练习,但有一点我被卡住了 我想用七种不同的数组测量3种排序算法(冒泡、插入和选择)的排序时间。我尝试了多种方法,但我无法在一个步骤中测量所有阵列的所有三种算法的排序时间。我的方案应: 用整数随机填充数组 分类 衡量时代 印刷《泰晤士报》 结果总是0毫秒,但不可能是这样,因为我尝试了100万整数数组,所以不可能达到0毫秒。最后,我尝试了“for loop”来实现我将要做的事情 我应该写下我所有的方法,因为你可能会在其他方法中发现另一个错误 public static void ra
- 用整数随机填充数组
- 分类
- 衡量时代
- 印刷《泰晤士报》
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);
}
}
}
publicstaticvoidrandomlyFillArray(int[]数组,int a,int b){
for(int i=0;i数组[j]){
//…交换元素
int temp=数组[i];
数组[i]=数组[j];
数组[j]=温度;
}
}
}
}
公共静态void insertionSort(int[]数组){
int i,j,温度;
对于(i=1;i0&&array[j-1]>temp){
数组[j]=数组[j-1];
j--;
}
数组[j]=温度;
}
}
公共静态void bubbleSort(int[]数组){
布尔交换=真;
int j=0;
内部温度;
while(交换){
交换=假;
j++;
对于(int i=0;i数组[i+1]){
温度=阵列[i];
数组[i]=数组[i+1];
阵列[i+1]=温度;
交换=真;
}
}
}
}
公共静态void main(字符串[]args){
//int[]array500=new int[500];//这些是我的数组,我应该对它们进行处理//每一个。
//int[]数组1000=新int[1000];
//int[]数组5000=新int[5000];
//int[]数组10000=新int[10000];
//int[]数组50000=新int[50000];
//int[]数组100000=新int[100000];
//整数[]数组500000=新整数[500000];
//int[]数组1000000=新int[1000000];
对于(int i=0;i<4;i++){
int[]数组=新的int[500];
如果(i==1){
随机填充数组(数组,11000);
选择排序(数组);
long startTime=System.currentTimeMillis();
长总计=0;
长停止时间=System.currentTimeMillis();
长延时=停止时间-开始时间;
选择排序(数组);
System.out.println(“500整数的选择排序:”
+延迟时间);
}else如果(i==2){
随机填充数组(数组,11000);
插入排序(数组);
long startTime=System.currentTimeMillis();
长总计=0;
长停止时间=System.currentTimeMillis();
长延时=停止时间-开始时间;
插入排序(数组);
System.out.println(“500整数的InsertionSort:
+延迟时间);
}else如果(i==3){
随机填充数组(数组,11000);
气泡排序(数组);
long startTime=System.currentTimeMillis();
长总计=0;
长停止时间=System.currentTimeMillis();
长延时=停止时间-开始时间;
气泡排序(数组);
System.out.println(“500整数的BubbleSort:”
+延迟时间);
}
}
}
谢谢你给我们全家的建议
诚恳地说。您所有的计时块都是这样的:
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);
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
}
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);
}
排序,然后取开始时间,然后立即取结束时间,然后再次排序,然后打印时间。您必须在排序后计算结束时间。如果您有理由进行两次排序(“预热”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);
}
}
for(int num:newint[]{50010005000,…}){
对于(int i=0;i<4;i++){
字符串排序=null;
int[]数组=新的int[num];
随机填充数组(数组,11000);
long startTime=System.currentTimeMillis();
如果(i==1){
sort=“SelectionSort”;
选择排序(数组);
}else如果(i==…){
//类似于其他种类
}
长停止时间=System.currentTimeMillis();
长延时=停止时间-开始时间;
System.out.println(对于“+num+”整数,“+elapsedTime”的排序+”;
}
}
创建微基准可能很棘手,因为JIT编译经常使用的代码,这需要花费一些时间
所以我的方法通常是这样的:
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);
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
}
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);
}
如果您使用的是Java8,您甚至可以创建如下内容:
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);
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
}
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);
}
publicstaticvoidmeasuresorting(int[]数组,使用者排序函数){
//这应该足够大
漫长的开始,漫长的结束;
随机填充数组(数组,11000);
//预热JIT-执行时无需测量
排序函数