Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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


This post first appeared on Let's Learn Java API, please read the originial post: here

Share the post

Compute Elapsed Time in Nanoseconds

×

Subscribe to Let's Learn Java Api

Get updates delivered right to your inbox!

Thank you for your subscription

×