Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8187353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:32:00+00:00 2026-06-07T02:32:00+00:00

I have an algorithm and I want to measure the time it works, but

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:32:02+00:00Added an answer on June 7, 2026 at 2:32 am

    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?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an algorithm that recursively makes change in the following manner: public static
I have an algorithm to create a PDF file, but I want just to
I have developed an MFCC algorithm and want to cluster same species of animal
I have this algorithm that I want to implement on VB6. Sub Main() dim
I am an experienced C++ programmer but new to Java. I have an algorithm
I have implemented MFCC algorithm and want to implement BFCC. What are the differences
I was wondering, I have an algorithm of mine that I want to test
I have an algorithm, and I want to figure it what it does. I'm
I have made small algorithm and want to implement it using javascript. Here is
I have code written in c, which contains some complex algorithms and I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.