What is the difference between System.currentTimeMillis and System.nanoTime?

Asked 1 years ago, Updated 1 years ago, 61 views

What is the difference between System.currentTimeMillis and System.nanoTime?

java timer time-precision

2022-09-22 16:21

1 Answers

In the system, "time" is used for two main purposes. The first is literally for expressing time (hours/minutes/seconds), and the second is for timers for scheduling or event processing. David Holmes of Oracle also described each as "passive clock" and "active clock." The methods that return each of these two times are currentTimeMillis and nanooTime.

First, let's look at the results of the currentTimeMillis method with a simple code.

public class CurrentTimeMillisTest {
        public static void main(String[] args) {
            long currentTime = System.currentTimeMillis();
            System.out.println("current time value = " + currentTime);
            System.exit(0);
        }
    }

current time value = 1335895964247

The currentTimeMillis method displays the UTC time, i.e., the time counted from midnight on January 1, 1970 to the present in milliseconds (ms). Therefore, the output shows that a total of 1335895964247 (ms) has passed since the start of UTC on January 1, 1970. Calculating each of these values in hours/minutes/second gives the current time (GMT+0).

Let's calculate the execution time of a particular code with the currentTimeMillis method.

public class CurrentTimeMillisTest {
        public static void doHardWork(int n, int m) {
            int result = 1;
            for(int i=0; i<n; i++)
                for(int j=0; j<m; j++)
                    for(int k=0; k<n+m; k++)
                        for(int l=0; l<n*m; l++)
                            result *= 2;
        }  
        public static void main(String[] args) {
            long startTime = System.currentTimeMillis();
            doHardWork(100, 100);
            long endTime = System.currentTimeMillis();

            long elapsedTime = endTime - startTime;
            System.out.println("Total elapsed time = " + elapsedTime);

            System.exit(0);
        }
    }

Total elapsed time = 11844 In the above code, I ran the doHardWork method, which requires some calculation time between startTime and endTime, and checked the results, and it was approximately 11.844 seconds. There seems to be no problem measuring the execution time of a particular code with the currentTimeMillis method. So why is the nanoTime method provided?

According to the reference, the nanoTime method currently returns the high-resolution time value of the Java virtual machine in ns (nanosec.), indicating that it should only be used to measure elapsed time and has no relation to the system or time (hour/minute/second). Then let's calculate the return value of the nanoTime method and the execution time of a particular code with a simple test code.

public class NanoTimeTest {
        public static int doHardWork(int n, int m) {
            int result = 1;
            for(int i=0; i<n; i++)
                for(int j=0; j<m; j++)
                    for(int k=0; k<n+m; k++)
                        for(int l=0; l<n*m; l++)
                            result *= 2;
            return result;
        }
        public static void main(String[] args) {
            long startTime = System.nanoTime();
            System.out.println("start nano-time = " + startTime);

            doHardWork(100, 100);  
            long endTime = System.nanoTime();
            System.out.println("end nano-time = " + endTime);     

            long elapsedTime = endTime - startTime;
            System.out.println("total elapsed time = " + elapsedTime);     
            System.exit(0);
        }
    }

start nano-time = 583086181421940 end nano-time = 583098045092043 total elapsed time = 11863670103

The output shows that approximately 11.86 seconds have elapsed.

To summarize the two methods based on references and other references,

Use the nanoTime method to calculate the execution time of the code, and the currentTimeMillis method to calculate the time (hour/minute/second)!!

http://mussebio.blogspot.kr/2012/05/java-api.html


2022-09-22 16:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.