The billionth of a second is known as a nanosecond. System.nanoTime() helps to get the time in nanoseconds. Nanosecond is used to read and write access time to RAM. It can also be used to gather information about the time taken by a coding task to complete its execution.
This article discusses the System.nanoTime() function and explains it with the help of examples.
How to Use the System.nanoTime() Function in Java?
The System.nanoTime() Function in Java returns the present time value of the current Java program with very high precision. It can be used to retrieve information regarding the elapsed time.
Syntax
public static long nanoTime()
Parameters
No parameters are required by the nanoTime() function.
Return Value
The return value is a long that provides information about the variation in the present time and that of midnight on January 1, 1970, UTC.
Example 1: Getting Current Time With Greater Precision
In this example, System.nanoTime() fetches the current time in high resolution. The following code explains the usage of using System.nanoTime():
import java.lang.*;
public class nanotime {
public static void main(String args[]) {
long nt = System.nanoTime();
System.out.println("Time in nanoseconds will be : "+nt);
}
}
In the above code:
- import java.lang.* imports the Java library.
- The public class is called nanotime, and it contains a method for obtaining the current time in nanoseconds.
- The current system is stored in a long type variable “nt”.
- System.out.println prints the most accurate system timer value currently available, in nanoseconds.
Output
The following output displays how the most precise present time can be fetched through the System.nanoTime() Function in Java:
Example 2: Measuring Execution Time Difference With nanoTime()
This example calculates the difference between the value returned by System.nanoTime() and the time required for the loop to complete 5000 iterations. The duration of a loop’s execution can be calculated using this method. The present values of time before and after the execution of the loop are fetched and their difference is taken.
Code
The following code explains how to find the time taken by a loop to complete all the iterations in nanoseconds using System.nanoTime() Function:
import java.lang.*;
public class nanotime {
public static void main(String args[]) {
long nt1 = System.nanoTime();
for(int i=0; i < 5000; i++) {
}
long nt2 = System.nanoTime();
long diff = nt2 - nt1;
System.out.println("Time taken by for loop in nanoseconds will be : " + diff );
}
}
In the above code:
- import java.lang.* imports the Java library.
- The public class is named nanotime and contains the method to get the time taken by the loop to complete the iterations in nanoseconds.
- The system’s current time, measured in nanoseconds, is stored in the long nt1.
- The long nt2 stores the current time in nanoseconds after the execution of the loop. The loop will be executed after completing 5000 iterations.
- nt2 – nt1 gets the difference of current time values before and after the loop execution in nanoseconds. A long-named diff file is used to store the outcome.
- The result which is the time the loop has taken to complete will be displayed using System.out.println
Output:
The following output displays the time taken by the loop to complete its execution in nanoseconds:
Conclusion
The System.nanoTime() Function in Java returns the present time of a running Java program in nanoseconds with greater precision. The functions get the current value of the system’s default time with a very high resolution. It can be used to know the execution time of the program. This article discussed the System.nanoTime() Function in Java and illustrated it with examples.