我有一个计时方法,它得到运行一个算法所需的时间。
public static long timing()
{
long startTime, endTime,elapsedTime;
startTime = System.nanoTime();
endTime = System.nanoTime();
elapsedTime = (endTime - startTime) ;
return elapsedTime ;
}假设我有两个排序算法,一个迭代合并排序调用
int[] copy12 = new int[values.length];
System.arraycopy(values,0,copy12,0,values.length);
iterativeMergeSort(copy12);
System.out.println("\nTime for iterative merge sort: " + timing() + " nanoseconds");
isSorted(copy12);
System.out.println("Iterative merge sort successfully sorted " + count + " elements");和值的插入排序调用
int[] copy9 = new int[values.length];
System.arraycopy(values,0,copy9,0,values.length);
insertSort(copy9);
System.out.println("\nTime for insertion sort: " + timing() + " nanoseconds");
System.out.println("insertion sort successfully sorted " + count + " elements");它们都有不同的时间,我试图找出运行速度最快的算法,然后像这样打印出来:
The best sorting method is insertion sort
It sorted all 1000 numbers in 656000 nanoseconds.在java中,我怎么可能采用这两个不同的时间,或者如果有更多的算法并比较它们,看哪一个更快?我已经搜索过了,但我没有找到这样的方法。
发布于 2016-02-07 05:30:13
根据这些评论,你的问题似乎包括两个部分:
1.计算函数的运行时间
您的timing()方法实际上做得不多。你真正想做的是:
long before = System.nanoTime();
someFunction();
long after = System.nanoTime();
System.out.println("Total elapsed time is " + (after-before) + " ns.");这将获得函数someFunction()的总运行时间(例如,在您的示例中是insertSort() )。它实际上存储了两个时间戳:一个是函数调用之前的,另一个是之后的。函数的运行时间将是这两种功能的不同。
这就是你如何将它转化为你的问题的方法:
// Time merge sort
long beforeMerge = System.nanoTime();
iterativeMergeSort(copy12);
long afterMerge = System.nanoTime();
long elapsedMerge = afterMerge - beforeMerge;
// Time insertion sort
long beforeInsert = System.nanoTime();
insertSort(copy9);
long afterInsert = System.nanoTime();
long elapsedInsert = afterInsert - beforeInsert;
// Print the elapsed time for each
System.out.println("Merge sort elapsed time = " + elapsedMerge + "ns.");
System.out.println("Insertion sort elapsed time = " + elapsedInsert + "ns.");
if(elapsedMerge<elapsedInsert)
System.out.println("Merge sort was faster");
else
System.out.println("Insertion sort was faster");2.在12个函数中找到最小的运行时间
现在我们可以计算一个函数的运行时间了,我们对所有12个函数都应用了相同的方法。其思想是将所有运行时间存储在一个数组中,然后使用一个简单的循环来确定哪个运行时间最快。
// This will contain the running times of the different sorting functions
long[] elapsed = new long[12];
// This will contain the names of the sorting functions
String[] names = new String[12];
long before, after; // used to compute the running times
// Time merge sort
before = System.nanoTime();
iterativeMergeSort(copy12);
after = System.nanoTime();
elapsed[0] = after-before;
names[0] = "Merge sort";
// Time insertion sort
before = System.nanoTime();
insertSort(copy9);
after = System.nanoTime();
elapsed[1] = after-before;
names[1] = "Insertion sort";
// ... Do the same for the others ...
// Determine the smallest running time
int fastest = 0;
for(int i=1; i<12; i++) {
if(elapsed[i]<elapsed[fastest])
fastest = i;
}
// Now the variable fastest contains the index of the fastest function
System.out.println("The fastest function was " + names[fastest] + ", with a running time of " + elapsed[fastest] + " nanoseconds.");这将打印如下内容:
The fastest function was Quicksort, with a running time of 12345 nanoseconds.发布于 2016-02-07 06:01:09
我想你最简单的回答是
Date date1;
Date date2;
if((date1.compareTo(date2))<0)
{
System.out.println("date1 is the lowest date");
}
else
{
System.out.println("date2 is the lowest date") ;
}如果compareTo函数的date1大于date2,则返回1;如果date1小于date2,则返回0。这个逻辑是比较两个使你自己的逻辑超过两个日期。我相信这会对你有帮助:)
https://stackoverflow.com/questions/35250037
复制相似问题