Saturday, April 17, 2010

Compute Elapsed Time in Nanoseconds


To compute some execution time we can use nanoTime() method in the System class. There are several other ways to calculate some execution time. But most accurate way is using nanoTime() method. nanoTime() method returns the current value which is a long value of the most precise system timer in Java. But this value is an approximate value.
//© learnjavaapi.blogspot.com
class ExeTime{
 public static void main(String srgs[]){
  long before=System.nanoTime();
  long sum=0;
  //some long execution
  for(int i=0;i<10000;i++){
   sum+=i;
  }
  long now=System.nanoTime();
  System.out.println("Time difference : " + (now-before) + " nS");
 }
}
Since : Java 1.5
Output :

References

0 comments:

Post a Comment

Stats

ලාංකීය සිතුවිලි

lankeeya sithuwili

Blog Catalogs

 

Let's Learn Java API. Copyright 2010 All Rights Reserved