Java 以纳秒为单位测量程序的运行时间
在测试各种文件大小的加密时,我应该测量下面代码的运行时间。然而,我认为我使用的方法只是输出CPU的当前计时器,而不是程序的运行时间。这是要实现的代码,但我认为我做得不对Java 以纳秒为单位测量程序的运行时间,java,algorithm,encryption,timer,aes,Java,Algorithm,Encryption,Timer,Aes,在测试各种文件大小的加密时,我应该测量下面代码的运行时间。然而,我认为我使用的方法只是输出CPU的当前计时器,而不是程序的运行时间。这是要实现的代码,但我认为我做得不对 long startTime = System.nanoTime(); // call function obj.callFunction(); long endTime = System.nanoTime(); long timeDifference = endTime - startTime; 这是我的实际计划。我应该
long startTime = System.nanoTime();
// call function
obj.callFunction();
long endTime = System.nanoTime();
long timeDifference = endTime - startTime;
这是我的实际计划。我应该如何修改定时器代码,使其反映程序的实际运行时间,而不是系统时钟?谢谢大家!
import javax.crypto.Cipher;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.SecretKeySpec;
public class AESJava {
public static void main(String[] args) {
// returns the current value of the system timer, in nanoseconds
System.out.print("time in nanoseconds = ");
System.out.println(System.nanoTime());
try {
BufferedReader br = new BufferedReader(new FileReader("key.txt"));
String key = br.readLine();
br.close();
FileInputStream fis = new FileInputStream("original.txt");
FileOutputStream fos = new FileOutputStream("encrypted.txt");
encrypt(key, fis, fos);
FileInputStream fis2 = new FileInputStream("encrypted.txt");
FileOutputStream fos2 = new FileOutputStream("decrypted.txt");
decrypt(key, fis2, fos2);
} catch (Throwable e) {
e.printStackTrace();
}
}
public static void encrypt(String key, InputStream is, OutputStream os) throws Throwable {
encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);
}
public static void decrypt(String key, InputStream is, OutputStream os) throws Throwable {
encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
}
public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {
SecretKeySpec dks = new SecretKeySpec(key.getBytes(),"AES");
Cipher cipher = Cipher.getInstance("AES");
if (mode == Cipher.ENCRYPT_MODE) {
cipher.init(Cipher.ENCRYPT_MODE, dks);
CipherInputStream cis = new CipherInputStream(is, cipher);
doCopy(cis, os);
} else if (mode == Cipher.DECRYPT_MODE) {
cipher.init(Cipher.DECRYPT_MODE, dks);
CipherOutputStream cos = new CipherOutputStream(os, cipher);
doCopy(is, cos);
}
}
public static void doCopy(InputStream is, OutputStream os) throws IOException {
byte[] bytes = new byte[128];
int numBytes;
while ((numBytes = is.read(bytes)) != -1) {
os.write(bytes, 0, numBytes);
}
os.flush();
os.close();
is.close();
}
}
我很困惑,因为你的第一件事是绝对正确的,但是你的主要方法没有做到你写的。它只打印CPU时间(从历元开始),不计算实际的墙时间。更改程序最简单的方法是创建一个方法(函数),它执行主方法所执行的操作,然后简单地插入初始部分。以下是摘录:
private static void doMain(){
try {
BufferedReader br = new BufferedReader(new FileReader("key.txt"));
String key = br.readLine();
br.close();
FileInputStream fis = new FileInputStream("original.txt");
FileOutputStream fos = new FileOutputStream("encrypted.txt");
encrypt(key, fis, fos);
FileInputStream fis2 = new FileInputStream("encrypted.txt");
FileOutputStream fos2 = new FileOutputStream("decrypted.txt");
decrypt(key, fis2, fos2);
} catch (Throwable e) {
e.printStackTrace();
}
}
那么您的主要方法就是:
public static void main(String ...args){
long startTime = System.nanoTime();
// call function
doMain();
long endTime = System.nanoTime();
long timeDifference = endTime - startTime;
}
计算平均时间的一种更好的方法是多次计算(需要纳秒精度的单次运行不会非常精确)。所以你可以这样做:
public static void main(String ...args){
long startTime = System.nanoTime();
// call function
for(int i = 0; i < NUM; ++i)
doMain();
long endTime = System.nanoTime();
long timeDifference = endTime - startTime;
double avgTime = (double)timeDifference / (double)NUM;
}
publicstaticvoidmain(字符串…参数){
long startTime=System.nanoTime();
//调用函数
对于(int i=0;i
我将强烈警告你,尽管这可能并不准确。在尝试计时时,有两件事需要注意:1)操作系统是否会假设数据已经在RAM中,2)体系结构是否会假设数据已经在缓存中。如果您多次运行此操作,您可能会以RAM的速度运行,而不是以读/写磁盘的速度运行。要真正获得平均速度,您需要确保操作系统必须读/写磁盘(这可能需要许多中间的“虚拟”读/写)。如果不使用底层操作系统的内核统计信息,您的应用程序就无法分辨差异。但是,如果多次运行代码,则应获得合理的值。若要添加到PMF的建议中,只要每次代码/数据相同,请多次运行代码,并采取最小的度量值,以最小化上下文切换和其他操作系统功能的时间。可能的重复: