在纳秒中测量程序的运行时间

时间:2014-03-26 05:37:31

标签: java algorithm encryption timer aes

在测试各种加密文件大小时,我应该测量下面代码的运行时间。但是我认为我使用的方法只是输出CPU的当前计时器而不是程序的运行时间。这是要实施的代码,但我不认为我做得正确

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();
    }

}

1 个答案:

答案 0 :(得分:1)

我很困惑,因为你的第一件事是绝对正确的,但是你的主要方法不会做你所写的。它只打印CPU时间(自纪元以来)并且不计算实际的挂壁时间。更改程序的最简单方法是创建一个方法(函数),它执行main方法的操作,然后只需插入初始部分。这是一段摘录:

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;
}

我会强烈提醒你,尽管这可能不准确。在尝试计时时,您需要注意两件事:1)OS是否假设数据已经在RAM中; 2)架构是否假设数据已经在缓存中。如果你多次运行,你可能会以读取/写入磁盘的速度以RAM的速度和而不是运行。要真正获得平均速度,您需要确保操作系统必须读/写磁盘(可能需要许多中间“虚拟”读/写)。