Download Free English Song Albums Here

Friday

Java Simple Example-2

Java example to calculate the execution time:

get execution time

This example program will describe you the way that how one can calculate or get the execution time of a method or program in java. This can be done by subtracting the starting time of the program or method by the ending time of the method. In our example java program we are using System.currentTimeMillis() method for getting current time in milliseconds.

We have created a method callMethod() and before calling this method we have stored the current time in a variable startTime and after the method is done with its working we have stored the current time into another variable endTime. Now by subtracting the startTime with endTime we can calculate the execution time of method.
Here is the full example code of GetExecutionTime.java as follows:


GetExecutionTime.java


import java.util.*;
public class GetExecutionTimes
{
  public GetExecutionTimes(){}
  public static void main(String args[])
  {
  long startTime = System.currentTimeMillis();
  GetExecutionTimes ext=new GetExecutionTimes();
  ext.callMethod();
  long endTime = System.currentTimeMillis();
  System.out.println("Total elapsed time in execution of
   method callMethod() is :"
(endTime-startTime));
  }
  public void callMethod(){
  System.out.println("Calling method");
  for(int i=1;i<=10;i++)
  {
  System.out.println("Value of counter is "+i);
  }
  }
}
  
Output

C:\javaexamples>javac GetExecutionTimes.java

C:\javaexamples>java GetExecutionTimes
Calling method
Value of counter is 1
Value of counter is 2
Value of counter is 3
Value of counter is 4
Value of counter is 5
Value of counter is 6
Value of counter is 7
Value of counter is 8
Value of counter is 9
Value of counter is 10
Total elapsed time in execution of method callMethod() is :16



Related Post:

No comments:

Post a Comment

More Posts

Justin Bieber Albums Download - Under the Mistletoe 2011 Justin Bieber feat. Usher - The Christmas Song Justin bieber 2011 New Album And Top Songs Justin Bieber Albums - Never Say Never - The Remixes 2011 Justin bieber Albums - Justin Bieber - My Worlds The Collection (Acoustic) 2010 Justin Bieber Songs - My world 2.0 (2010) - Justin Bieber - Mp3.320 kbps.VBR - Inc Covers & bonus tracks -Mediafire
Justin Bieber Albums Download - Under the Mistletoe 2011 Justin Bieber feat. Usher - The Christmas Song Justin bieber 2011 New Album And Top Songs Justin Bieber Albums - Never Say Never - The Remixes 2011 Justin bieber Albums - Justin Bieber - My Worlds The Collection (Acoustic) 2010 Justin Bieber Songs - My world 2.0 (2010) - Justin Bieber - Mp3.320 kbps.VBR - Inc Covers & bonus tracks -Mediafire
50 Cent Albums Download (8) Akon Albums Download (11) Albums Released This November (17) Albums Released This October (24) Ashley Tisdale Albums Download (2) Avril Lavigne Albums Download (4)
+ Grab this