Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Java example to calculate the 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.

 

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

Download Source Code

                         

» View all related tutorials
Related Tags: java c error com diff io user sed print output stream display command page exec uri int exe move if

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.