Class for program event timing : Timing « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » TimingScreenshots 
Class for program event timing
   

/*
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 
 * MA  02111-1307, USA.
 */
//package no.geosoft.cc.util;



import java.util.Date;



/**
 * Class for program event timing.
 * Usage:
 *
 *   <pre>
 *   Timer timer = new Timer();
 *
 *   // do stuff
 *
 *   System.out.println (timer);  // prints time elapsed since
 *                                // object was created.
 *   </pre>
 
 @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
 */
public class Timer
{
  private Date  start_;
  

  
  /**
   * Start timer.
   */
  public Timer()
  {
    reset();
  }


  
  /**
   * Returns exact number of milliseconds since timer was started.
   
   @return  Number of milliseconds since timer was started.
   */
  public long getTime()
  {
    Date now = new Date();
    long nMillis = now.getTime() - start_.getTime();

    return nMillis;
  }


  
  /**
   * Restarts the timer.
   */
  public void reset()
  {
    start_ = new Date();  // now    
  }


  
  /**
   * Returns a formatted string showing the elaspsed time
   * suince the instance was created.
   
   @return  Formatted time string.
   */
  public String toString()
  {
    long nMillis = getTime();
    
    long nHours   = nMillis / 1000 60 60;
    nMillis -= nHours * 1000 60 60;
      
    long nMinutes = nMillis / 1000 60;
    nMillis -= nMinutes * 1000  60;

    long nSeconds = nMillis / 1000;
    nMillis -= nSeconds * 1000;
    
    StringBuffer time = new StringBuffer();
    if (nHours > 0time.append (nHours + ":");
    if (nHours > && nMinutes < 10time.append ("0");
    time.append (nMinutes + ":");
    if (nSeconds < 10time.append ("0");
    time.append (nSeconds);
    time.append (".");
    if (nMillis < 100time.append ("0");
    if (nMillis <  10time.append ("0");
    time.append (nMillis);
    
    return time.toString();
  }


  
  /**
   * Testing this class.
   
   @param args  Not used.
   */
  public static void main (String[] args)
  {
    Timer timer = new Timer();

    for (int i = 0; i < 100000000; i++) {
      double b = 998.43678;
      double c = Math.sqrt (b);
    }

    System.out.println (timer);
  }
}
    
    
  
Related examples in the same category
1. Compute and display elapsed time of an operation
2. Get system time using System class
3. Get elapsed time in milliseconds
4. Get elapsed time in seconds
5. Get elapsed time in minutes
6. Get elapsed time in hours
7. Get elapsed time in days
8. Get time in milliseconds using Java Calendar
9. java.util.concurrent.TimeUnit: convert between milliseconds and days
10. Get seconds since
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.