Home > Back-end >  How to display the build time in miliseconds? (JAVA)
How to display the build time in miliseconds? (JAVA)

Time:09-28

Hi guys I made this code so far, but I need to display the execution time into miliseconds using System.currentTimeMillis(), but am not sure how!

public class Assignment4 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        long value = System.currentTimeMillis(); 
        int n=10;
        for(int i=0;i<n;i  )
           System.out.println(i);     
    }
}

CodePudding user response:

You need to get the timestamp at the end of the execution and subtract the initial one.

public class Assignment4 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        long value = System.currentTimeMillis();
        int n = 10;
        for (int i = 0; i < n; i  )
            System.out.println(i);
        long totalTime =  System.currentTimeMillis()-value;
        System.out.println(totalTime);
    }
}

CodePudding user response:

You can store the start and end time of the program, and print out their difference.

public static void main(String[] args) {
    long start = System.currentTimeMillis(); 
    int n=10;
    for(int i=0;i<n;i  )
       System.out.println(i);    
    long end = System.currentTimeMillis();
    System.out.println(end - start);
}
  • Related