Java System.nanoTime() vs System.currentTimeMillis


What are the Time Operations in Java?

There are two time operations provided by Java environment. For the time related operation, users can use these operations.

  • System.nanoTime()

  • System.currentTimeMillis()

System.nanoTime() mainly known as expensive call, used to get more specific value of time. And, System.currentTimeMillis() most authentic possible passed time, helps to get the time value based on the operating system basically. The first function returns the time value in Nano Seconds (can give negative value sometimes) and on the other hand second function in milliseconds. So it's obvious, System.nanoTime() is one step ahead and more acceptable for its accuracy.

What are System.nanoTime() and System.currentTimeMillis()

  • The System.nanoTime() method in Java Environment helps to find the difference out at two pointers.

  • In simple words, it helps to get a time reading before the process starts and another after executing the method.

  • It is not recommended every time to profile a code because the time may differ based on the OS of a system. So, we can experience some inaccuracy.

  • The System.currentTimeMillis(), is a thread safe method which will no return ambiguous results.

  • This function returns date, time and year as time.

  • Where the reference is fixed, there we can get accurate output. But when a user make some changes in system time, it will give wrong results at that moment.

Algorithm for System.nano.Time()

  • Step 1 − Load Libraries.

  • Step 2 − Declare Main Method by Using System.nanoTime().

  • Step 3 − Display The Table Of 2.

  • Step 4 − The Loop Will Produce Multiple of 2 (2x Multiple).

  • Step 5 − Print Output from the given Input.

Algorithm for System.currentTimeMillis()

  • Step 1 − Load Libraries.

  • Step 2 − Declare Main Method By Using TimeMillis() Method.

  • Step 3 − Display the Milliseconds, Minutes, Hours and Days.

  • Step 4 − Print Output.

Syntax for System.currenyTimeMillis()

System.currentTimeMillis();
--> java.language Package
--> Declare System Class
--> currentTimeMillis() Method 

Syntax for System.nanoTime()

public static long nanoTime() 
long process startTime = System.nanoTime(); 
long process estimatedTime = System.nanoTime() - startTime;

Comparison of System.nanoTime() and System.currentTimeMillis():-

Here is a general example for the two specific Java time operations -

package javalangtimeslot;
import java.lang.*;
public class javatimeslot {

   public static void main(String[] args) { 
      System.out.print("Time in nanoseconds we can see here = ");
      System.out.println(System.nanoTime());
      System.out.print("Time in milliseconds we can see here = "); 
      System.out.println(System.currentTimeMillis());
   }
}  

Output

Time in nanoseconds we can see here = 4083437933186033
Time in milliseconds we can see here = 1680778649732

Different Approaches to Follow

  • Approach 1 − Java Program using System.currentTimeMillis() Method

  • Approach 2 − Java Program using Java System.nanoTime()

Java Program using System.currentTimeMillis() Method

System.currentTimeMillis() Method return current time in milli second unit. This method times how much time a part of a code takes to execute.

Example 1

import java.io.*;
public class timefuncjava {
	public static void main(String[] args){
		System.out.println("Milliseconds Are Here : " + System.currentTimeMillis());
		System.out.println("Seconds Are Here: " + (System.currentTimeMillis())/ 2000);
		System.out.println("Minutes Are Here: " + (System.currentTimeMillis())/ 2000 / 120);	
		System.out.println("Hours Are Here: " + (System.currentTimeMillis())/ 2000 / 120 / 120);
		System.out.println("Days Are: " + (System.currentTimeMillis())/ 2000 / 120 / 120 / 48);	
		System.out.println("Years Total: " + (System.currentTimeMillis()) / 2000 / 120 / 120 / 48 / 365);
        System.out.println("Milliseconds Are Here : " + System.currentTimeMillis());
		System.out.println("Seconds Are Here: " + (System.currentTimeMillis())/ 2000);
		System.out.println("Minutes Are Here: " + (System.currentTimeMillis())/ 2000 / 120);	
		System.out.println("Hours Are Here: " + (System.currentTimeMillis())/ 2000 / 120 / 120);
		System.out.println("Days Are: " + (System.currentTimeMillis())/ 2000 / 120 / 120 / 48);	
		System.out.println("Years Total: " + (System.currentTimeMillis()) / 2000 / 120 / 120 / 48 / 365);

	}
}

Output

Milliseconds Are Here : 1680778875055
Seconds Are Here: 840389437
Minutes Are Here: 7003245
Hours Are Here: 58360
Days Are: 1215
Years Total: 3
Milliseconds Are Here : 1680778875062
Seconds Are Here: 840389437
Minutes Are Here: 7003245
Hours Are Here: 58360
Days Are: 1215
Years Total: 3

Here in this build code we have used the TimeMillis to get milliseconds, seconds, minutes, hours, days and years.

Example 2

public class timeprogjava{
   public static void main(String args[]){
      long starting, ending;
      System.out.println("Timing a loop from 0 to 100,000,000,000");
      starting= System.currentTimeMillis(); 
      for(long k=0;k<100000000000L;k++);
      ending=System.currentTimeMillis(); 
      System.out.println("Elapsed time Is: "+(ending-starting));
   }
}

Output

Timing a loop from 0 to 100,000,000,000

Java Program using System.nanoTime() Method

System nanoTime() Method is a Java Environment, which is a high definition time pool in the nano second unit. This method always returns current value.

Example 1

Using nano Time() -difference, we can calculate the consumed time by an empty loop to run this for the next 1000 times. Thee time output we will get before and after of the loop.

public class javacodetime {
   public static void main(String args[]) {
      long nanoTime2 = System.nanoTime();
      for(int k=0; k < 10000; k++) {
             
      }
      long nanoTime5 = System.nanoTime();    
      long runTimeInNanoSeconds = nanoTime5 - nanoTime2;    

      System.out.println("Time taken to execute the process by for loop : " + runTimeInNanoSeconds + " nano seconds");   
   }  
}

Output

Time taken to execute the process by for loop : 132509 nano seconds

Example 2

This example will help us to get the current time by using System.nanoTime() method in nano seconds.

public class javatimecode {
   public static void main(String args[]) {
      long nanoTime = System.nanoTime();       
      System.out.println("Current time as in Nano Seconds : "+nanoTime);
   
   }  

}

Output

Current time as in Nano Seconds : 4084089973012410

Conclusion

In conclusion, the System.nano Time() method is required to use when the problem is too heavy. For the fast performance like HD games, the nano time operation is the best option to avail. But for the accurate output we can use System.currentTimeMillis().

In this article we have learned how to get the time by using the two time methods. We also built some Java code by using the logic.

Updated on: 10-Apr-2023

732 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements