Java : System Time and Calculate Processing Time

System.currentTimeMillis(): Returns the current time in milliseconds. The granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

System.nanoTime(): Returns the current value of the running Java Virtual Machine’s high-resolution time source, in nanoseconds. This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time.

These methods are used to calculate processing time or performance of operations , methods and apis.

Processing Time = End Time – Start Time ;

Check below example to calculate processing time :

Example

public class SystemTimeCalculation {

public static void main(String[] args) {
long startTime=System.currentTimeMillis();
long nanoStartTime=System.nanoTime();
int i=0;
while(i++<1000000);
long endTime=System.currentTimeMillis();
long nanoEndTime=System.nanoTime();
System.out.println("System Calculate Time:"+(endTime-startTime));
System.out.println("System Calculate Nano Time:"+(nanoEndTime-nanoStartTime));

}

}

Output


System Calculate Time:2
System Calculate Nano Time:1888263
Advertisements

This site uses Akismet to reduce spam. Learn how your comment data is processed.