如何测量算法的时间

时间:2012-07-07 20:54:30

标签: java time

我有一个算法,我想测量它的工作时间,但我得到0。 怎么解决?开始和结束是一样的。

public static String MD5(String message) {
    try {
        long start = System.currentTimeMillis();
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(message.getBytes());
        byte[] hashBytes = md5.digest();

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < hashBytes.length; i++) {
            sb.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16)
                    .substring(1));
        }
        long end = System.currentTimeMillis();
        MD5TIME = end - start;
        System.out.println(end);
        System.out.println(start);
        return sb.toString();

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

修改

但现在不幸的是,当我写入静态:MD5和SHA-1进入文件时,它们仍然是1和2。

    import java.io.BufferedWriter;
import java.security.MessageDigest;

public class ShortCuts {
volatile static long  MD5TIME = 1, SHA1TIME = 2;
    public static String MD5(String message) {
        try {
            long start = System.nanoTime();
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(message.getBytes());
            byte[] hashBytes = md5.digest();

            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < hashBytes.length; i++) {
                sb.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16)
                        .substring(1));
            }
            long end = System.nanoTime();
            MD5TIME = end - start;
            System.out.println(MD5TIME);
            return sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String SHA(String message) {
        long start = System.nanoTime();
        int[] t = SHA1.prepareDataForSHA1(message);
        SHA1TIME = System.nanoTime() - start;
        return SHA1.doSHA1(t);
    }

    public static void addShortcutsIntoTheFile(BufferedWriter bw, String message) {
        try {
            bw.newLine();
            bw.write("MD5");
            bw.newLine();
            System.out.println(MD5TIME);
            bw.write("TIME: " + MD5TIME);
            bw.newLine();
            bw.write(ShortCuts.MD5(message));
            bw.newLine();
            bw.newLine();
            bw.write("SHA");
            bw.newLine();
            bw.write("TIME: " + SHA1TIME);
            bw.newLine();
            bw.write(ShortCuts.SHA(message));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

输出

    MD5
TIME: 1
c4909803cf840c8cf0556e16e4cc1483

SHA

TIME: 2
7d3f446eef84b651dc7b519fe5ad4157279cb45e

CONSOLE

794076

4 个答案:

答案 0 :(得分:5)

使用System.nanoTime()进行更精确的测量

注意:

  

此方法只能用于测量经过时间,与系统或挂钟时间的任何其他概念无关


另见

答案 1 :(得分:3)

或者更好的是,使用专为微基准设计的工具:Google's Caliper

How do I write a correct micro-benchmark in Java?

答案 2 :(得分:2)

您可以提高计时器的分辨率。但是如果你得到的数字(以毫安为单位)为零,那么你可以通过计时多次执行功能(1000+)来获得更准确的答案。

答案 3 :(得分:0)

更正后的代码,工作正常:

    import java.io.BufferedWriter;
import java.io.IOException;
import java.security.MessageDigest;

public class ShortCuts {
volatile static long  MD5TIME = 1, SHA1TIME = 2;
    public static String MD5(String message,BufferedWriter bw) {
        try {
            long start = System.nanoTime();
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(message.getBytes());
            byte[] hashBytes = md5.digest();

            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < hashBytes.length; i++) {
                sb.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16)
                        .substring(1));
            }
            long end = System.nanoTime();
            MD5TIME = end - start;
            System.out.println(MD5TIME);
            bw.newLine();
            System.out.println(MD5TIME);
            bw.write("TIME: " + MD5TIME);
            bw.newLine();
            return sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String SHA(String message, BufferedWriter bw) throws IOException {
        long start = System.nanoTime();
        int[] t = SHA1.prepareDataForSHA1(message);
        SHA1TIME = System.nanoTime() - start;
        bw.newLine();
        bw.write("TIME: " + SHA1TIME);
        bw.newLine();
        return SHA1.doSHA1(t);
    }

    public static void addShortcutsIntoTheFile(BufferedWriter bw, String message) {
        try {
            bw.newLine();
            bw.write("MD5");

            bw.write(ShortCuts.MD5(message,bw));
            bw.newLine();
            bw.newLine();
            bw.write("SHA");
            bw.write(ShortCuts.SHA(message,bw));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

感谢您的回答。