Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what should i use

Status
Not open for further replies.

ironjedi

Programmer
Oct 16, 2002
1
US
Thanks in advance,
I've just began learning to program in java about a month ago and I have started this project and I was wondering what feature could and should i use in order to time events (say keyboard press release events) to the accuracy of milliseconds.
 
You could use the system.currentTimeMillis() function, here is some code i found on builder.com:



Debuggers are heavy clunky things and not always the most effective method available; sometimes, you want to add just a little bit of debugging and profiling to some code. One solution is to output timestamps with the help of a nice simple StopWatch class.
Code:
package com.generationjava.test;

/**
 * Useful when doing timings in a debug or test situation.
 */
public class StopWatch {

    static public int AN_HOUR = 60 * 60 * 1000;
    static public int A_MINUTE = 60 * 1000;

    private long startTime = -1;
    private long stopTime = -1;
    
    /**
     * Start the stopwatch.
     */
    public void start() {
        this.startTime = System.currentTimeMillis();
    }
    
    /**
     * Stop the stopwatch.
     */
    public void stop() {
        this.stopTime = System.currentTimeMillis();
    }

    /**
     * Reset the stopwatch.
     */
    public void reset() {
        this.startTime = -1;
        this.stopTime = -1;
    }
        
    /**
     * Split the time.
     */
    public void split() {
        this.stopTime = System.currentTimeMillis();
    }

    /**
     * Remove a split.
     */
    public void unsplit() {
        this.stopTime = -1;
    }
    
    /**
     * Get the time on the stopwatch. This is either the
     * time between start and latest split, between start and stop,
     * or the time between the start and the moment this method is called.
     */
    public long getTime() {
        if(stopTime != -1) {
            return (System.currentTimeMillis() - this.startTime);
        } else {
            return this.stopTime - this.startTime;
        }
    }
    
    public String toString() {
        return getTimeString();
    }
    
    /**
     * Get the time gap as a String.
     * In hours, minutes, seconds and milliseconds.
     */
    public String getTimeString() {
        int hours, minutes, seconds, milliseconds;
        long time = getTime();
        hours = (int) (time / AN_HOUR);
        time = time - (hours * AN_HOUR);
        minutes = (int) (time / A_MINUTE);
        time = time - (minutes * A_MINUTE);
        seconds = (int) (time / 1000);
        time = time - (seconds * 1000);
        millis = (int) time;
 
        return hours + "h:" + minutes + "m:" + seconds + "s:" + millis + "ms";
    }      
}
While that's a fair chunk of code, it's all very simple. It does just enough to make it reusable and nowhere near enough to make it complex. Using the StopWatch class is therefore very simple:
Code:
StopWatch obj = new StopWatch();
obj.start();
try {
    Thread.currentThread().sleep(1500);
} catch(InterruptedException ie) {
    // ignore
}
obj.stop();
System.out.println(obj);
We sleep for 1500 milliseconds, or 1.5 seconds, and unsurprisingly, the StopWatch reports:

0h:0m:1s:502ms




hope this helps
Alistair
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top