Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure time of Algorithm

Tags:

java

time

I have an algorithm and I want to measure the time it works, but I get 0. How to fix it? start and end are the same.

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

EDIT

But now unfortunately when I write statics: MD5 and SHA-1 into the file, they still are 1 and 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();
        }
    }

}

OUTPUT

    MD5
TIME: 1
c4909803cf840c8cf0556e16e4cc1483

SHA

TIME: 2
7d3f446eef84b651dc7b519fe5ad4157279cb45e

CONSOLE

794076
like image 705
Yoda Avatar asked Dec 26 '22 21:12

Yoda


2 Answers

Use System.nanoTime() for more precise measurement

Note:

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time


See Also

  • How to find time taken to run java program?
like image 118
jmj Avatar answered Dec 29 '22 12:12

jmj


Or better, use a tool designed for micro-benchmark: Google's Caliper

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

like image 38
Aravind Yarram Avatar answered Dec 29 '22 10:12

Aravind Yarram