为什么这些长整数被写为 0 或 1?
Why do these longs get written as 0's or 1's?
也许我的大脑今天不工作,但我只是想计算方法执行时间并将时间写入文件。出于某种原因,长值被写为 0 和 1。这是相关代码:
public static void main(String[] args) {
//Reads numbers from file
Scanner numberReader;
//Writes execution times to file
BufferedWriter writer;
String heading = "Euclid (ms)\t\tPrime Factor (ms)\n";
//Hold numbers from file
int num1, num2;
//Holds execution times
long startTime, endTime, elapsedTime;
try {
//Reads file
numberReader = new Scanner(new File(args[0]));
//Opens the file to write to
writer = new BufferedWriter(new FileWriter("comparison.txt", false));
writer.write(heading);
//While there are more numbers to read
while(numberReader.hasNextInt()) {
//Read the numbers
num1 = numberReader.nextInt();
num2 = numberReader.nextInt();
System.out.println("Numbers: " + num1 + ", " + num2);
//Run the first method and calculate execution time
startTime = System.currentTimeMillis();
System.out.println("Euclid GCD: " + euclid(num1, num2));
endTime = System.currentTimeMillis();
elapsedTime = endTime - startTime;
//Write execution time to file
writer.write(elapsedTime + "\n");
//Run the second method and calculate execution time
startTime = System.currentTimeMillis();
System.out.println("Prime Factor GCD: " + primeFactor(num1, num2));
endTime = System.currentTimeMillis();
elapsedTime = endTime - startTime;
//Write execution time to file
writer.write(elapsedTime + "\n");
}
//Close the reader and writer
numberReader.close();
writer.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
这是一个示例输出:
Euclid (ms) Prime Factor (ms)
1
1
0
0
0
0
有什么想法吗?
您尝试测量的方法的执行时间约为 micro 秒。这些时间四舍五入为毫秒,因此任何小于 500µs 的时间都将输出为零。
您需要使用更高分辨率的计时器,例如 System.nanoTime()
,它可用于测量较短的时间间隔,但仅对经过的时间有用,对一天中的时间没有用。 IE。您可以减去两个值以获得经过的时间,但这些值本身与一天中的任何特定时间都不相关。
请注意,根据硬件的不同,您可能无法获得纳秒分辨率,但它会比毫秒好得多。
也许我的大脑今天不工作,但我只是想计算方法执行时间并将时间写入文件。出于某种原因,长值被写为 0 和 1。这是相关代码:
public static void main(String[] args) {
//Reads numbers from file
Scanner numberReader;
//Writes execution times to file
BufferedWriter writer;
String heading = "Euclid (ms)\t\tPrime Factor (ms)\n";
//Hold numbers from file
int num1, num2;
//Holds execution times
long startTime, endTime, elapsedTime;
try {
//Reads file
numberReader = new Scanner(new File(args[0]));
//Opens the file to write to
writer = new BufferedWriter(new FileWriter("comparison.txt", false));
writer.write(heading);
//While there are more numbers to read
while(numberReader.hasNextInt()) {
//Read the numbers
num1 = numberReader.nextInt();
num2 = numberReader.nextInt();
System.out.println("Numbers: " + num1 + ", " + num2);
//Run the first method and calculate execution time
startTime = System.currentTimeMillis();
System.out.println("Euclid GCD: " + euclid(num1, num2));
endTime = System.currentTimeMillis();
elapsedTime = endTime - startTime;
//Write execution time to file
writer.write(elapsedTime + "\n");
//Run the second method and calculate execution time
startTime = System.currentTimeMillis();
System.out.println("Prime Factor GCD: " + primeFactor(num1, num2));
endTime = System.currentTimeMillis();
elapsedTime = endTime - startTime;
//Write execution time to file
writer.write(elapsedTime + "\n");
}
//Close the reader and writer
numberReader.close();
writer.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
这是一个示例输出:
Euclid (ms) Prime Factor (ms)
1
1
0
0
0
0
有什么想法吗?
您尝试测量的方法的执行时间约为 micro 秒。这些时间四舍五入为毫秒,因此任何小于 500µs 的时间都将输出为零。
您需要使用更高分辨率的计时器,例如 System.nanoTime()
,它可用于测量较短的时间间隔,但仅对经过的时间有用,对一天中的时间没有用。 IE。您可以减去两个值以获得经过的时间,但这些值本身与一天中的任何特定时间都不相关。
请注意,根据硬件的不同,您可能无法获得纳秒分辨率,但它会比毫秒好得多。