Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-29-2004, 05:15 PM   #1 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
technobard is on a distinguished road
Timing Is Everything

Timing Is Everything
by Byron Lee aka Technobard

One of the most common questions I'm asked these days is "how long did it take to run" or the related "how long will it take to run". In every environment I work in, sooner or later I get around to figuring out how to do timings. In Java, I was hoping for something as simple as a stopwatch. You know, ready, set, CLICK........and..CLICK. 3.65 seconds..or something like that. Sometimes I want minutes, sometimes I want milliseconds. It all depends. And while there are other, more advanced features I'd like to have, the simple solution covers at least 80% of what I really need.

Let's take a look at the StopWatch class and a "main" method to test it.

CLICK...
Code:
import java.util.*;

public class StopWatch {
    Calendar startCal;
    Calendar endCal;
    TimeZone tz = TimeZone.getTimeZone("CST");
    
    /** Creates a new instance of StopWatch */
    public StopWatch() {
    }

    public StopWatch(String tzoneStr) {
	  tz = TimeZone.getTimeZone(tzoneStr);
    }
    
    // Start the stopwatch
    public void start() {
        startCal = Calendar.getInstance(tz);
    }
    
    // Stop the stopwatch
    public void end() {
        endCal = Calendar.getInstance(tz);
    }
    
    // Measure the elapsed time in different units
    public double elapsedSeconds() {
        return (endCal.getTimeInMillis() - startCal.getTimeInMillis())/1000.0;
    }
    
    public long elapsedMillis() {
        return endCal.getTimeInMillis() - startCal.getTimeInMillis();
    }
    
    public double elapsedMinutes() {
        return (endCal.getTimeInMillis() - startCal.getTimeInMillis())/(1000.0 * 60.0);
    }
    
    public static void main (String [] args) {
        StopWatch sw = new StopWatch();
        sw.start();  // capture start time
        
        try {
        Thread.sleep(5000);   // sleep for 5 seconds
        }catch (Exception e) {
		System.out.println(e);
            System.exit(1);
        }
        
        sw.end();  // capture end time
        
        System.out.println("Elapsed time in minutes: " + sw.elapsedMinutes());
        System.out.println("Elapsed time in seconds: " + sw.elapsedSeconds());
        System.out.println("Elapsed time in milliseconds: " + sw.elapsedMillis());
    }
}  // end of StopWatch class
The StopWatch class is fairly simple. It contains two Calendar objects: "startCal" and "endCal". Each Calendar object contains the date and time to the millisecond at the time each instance is created. A call to Calendar.getInstance() returns a new Calendar object. Take a look at the start() and end() methods. Once these are set, the remaining methods calculate elapsed time in various units.

CLICK. Time to go.
technobard is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java - Timing Is Everything sde Code Newbie News 0 07-30-2004 01:10 AM
Timing elbadry Standard C, C++ 2 06-27-2004 05:37 PM


All times are GMT -8. The time now is 01:23 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting