JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

Current Time in Milliseconds and Nanoseconds

This section provides a tutorial example on how to obtain the current time in milliseconds and nanoseconds using currentTimeMillis() and nanoTime() methods.

If you want to measure how long some code takes to execute, you can use System.currentTimeMillis() and System.nanoTime() methods. They returns the current time in milliseconds and in nanoseconds.

Here is tutorial example program to check the performance of the Math.sqrt() method:

/**
 * SystemCurrentTime.java
 * Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
 */
class SystemCurrentTime {
   public static void main(String[] a) {
      java.io.PrintStream out = System.out;
      out.println("Performance for 1000 operationonds:");
      out.println("   in milliseconds: "+milliseconds(1000));
      out.println("   in nanoseconds: "+nanoseconds(1000));

      out.println("Performance for 1000000 operationonds:");
      out.println("   in milliseconds: "+milliseconds(1000000));
      out.println("   in nanoseconds: "+nanoseconds(1000000));
   }
   public static long milliseconds(long max) {
      // Current time in milliseconds
      long time1 = System.currentTimeMillis();
      for (int i=0; i<max; i++){
        double input = 7.0;
        double output = Math.sqrt(input);
      }
      long time2 = System.currentTimeMillis();
      return time2 - time1;
   }
   public static long nanoseconds(long max) {      
      // Current time in nanoseconds
      long time1 = System.nanoTime();
      for (int i=0; i<max; i++){
        double input = 7.0;
        double output = Math.sqrt(input);
      }
      long time2 = System.nanoTime();
      return time2 - time1;
   }
}

When executed on my Windows XP system with JDK 1.6.0, I got this result:

C:\herong\jvm>java SystemCurrentTime
Performance for 1000 operationonds:
   in milliseconds: 0
   in nanoseconds: 144991
Performance for 1000000 operationonds:
   in milliseconds: 62
   in nanoseconds: 31467128

C:\herong\jvm>java SystemCurrentTime
Performance for 1000 operationonds:
   in milliseconds: 0
   in nanoseconds: 144990
Performance for 1000000 operationonds:
   in milliseconds: 63
   in nanoseconds: 41659764

C:\herong\jvm>java SystemCurrentTime
Performance for 1000 operationonds:
   in milliseconds: 0
   in nanoseconds: 144991
Performance for 1000000 operationonds:
   in milliseconds: 62
   in nanoseconds: 48716806

The test result tells me that:

  • nanoTime() method is very useful to measure very short durations.
  • currentTimeMillis() method seems to be more stable, giving about the same measurement on each run.
  • nanoTime() method seems to be less stable, giving different measurements on different runs.
  • 1 millisecond is 1,000,000 nanoseconds.

Last update: 2010.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

 java.lang.Runtime Class - The JVM Instance

java.lang.System Class - The Operating System

 What Is java.lang.System?

 Standard Input, Output, and Error Streams

Current Time in Milliseconds and Nanoseconds

 Accessing System Environment Variables

 Getting and Adding System Properties

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

 Memory Management Rules and Tests

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

 StringBuffer Testing Program and Result

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
Current Time in Milliseconds and Nanoseconds