Jump to content

Basic Timer classes


Botre

Recommended Posts

LAST UPDATE: MARCH 6, 2015

package org.bjornkrols.timing;

/**
 * A base for basic timer subclasses.
 * 
 * @author 	Bjorn Krols (Botre)
 * @version	0.1
 * @since 	March 6, 2015	
 */

public abstract class BasicAbstractTimer {

	protected long st;
	protected int mtups;

	protected BasicAbstractTimer() {
		start();
	}
	
	public void start() {
		st = fetchCurrentTime();
	}

	public void restart() {
		start();
	}
	
	protected abstract long fetchCurrentTime();

	public double getElapsedDays(){
		return getElapsedSeconds() / 86400;
	}
	
	public double getElapsedHours(){
		return getElapsedSeconds() / 86400;
	}
	
	public double getElapsedSeconds() {
		return (fetchCurrentTime() - st) / mtups;
	}
	
	public double getElapsedMilliSeconds() {
		return getElapsedSeconds() / 1000;
	}
	
	public double getElapsedNanoSeconds() {
		return getElapsedSeconds() / 1000000000;
	}
	
	public int[] distribute() {
		int seconds = (int) getElapsedSeconds();
		int d[] = new int[4];
		d[0] =  (seconds / 86400); 			// Days.
		d[1] =  ((seconds % 86400) / 3600); 		// Hours.
		d[2] =  (((seconds % 86400) % 3600) / 60); 	// Minutes.
		d[3] =  (((seconds % 86400) % 3600) % 60); 	// Seconds.
		return d;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		int[] d = distribute();
		for (int i = 0; i < d.length; i++) {
			sb.append(String.format("%02d", d[(int) i]));
			if (i != d.length - 1) sb.append(":");
		}
		return sb.toString();
	}

} 
package org.bjornkrols.timing;

/**
 * A millisecond precision timer.
 * 
 * @author 		Bjorn Krols (Botre)
 * @version		0.0
 * @since		March 6, 2015
 */

public final class BasicMilliTimer extends BasicAbstractTimer {
	
	public BasicMilliTimer() {
		super();
		mtups = 1000;
	}
	
	@Override
	protected long fetchCurrentTime() {
		return System.currentTimeMillis();
	}
	
	@Override
	public double getElapsedMilliSeconds() {
		return st - fetchCurrentTime();
	}

}
package org.bjornkrols.timing;

/**
 * A nanosecond precision timer.
 * 
 * @author 		Bjorn Krols (Botre)
 * @version		0.0
 * @since		March 6, 2015
 */

public final class BasicNanoTimer extends BasicAbstractTimer {

	public BasicNanoTimer() {
		super();
		mtups = 1000000000;
	}

	@Override
	protected long fetchCurrentTime() {
		return System.nanoTime();
	}
	
	@Override
	public double getElapsedNanoSeconds() {
		return st - fetchCurrentTime();
	}

}
Edited by Botre
Link to comment
Share on other sites

then remove the abstract and add in an argument to the constructor to allow the input of the current time.

 

Passing a method as a parameter is tedious imho.

I could make fetchCurrentTime() non abstract and have it take a long argument for current time and an int for converting to other time units but the current implementation is more efficient (I could also get rid of fetchCurrentTime() and make the getElaspsed...() methods take those parameters but again, no need for that).

 

Perhaps I misunderstood, if I did feel free to post some pseudocode :p

Link to comment
Share on other sites

Passing a method as a parameter is tedious imho.

I could make fetchCurrentTime() non abstract and have it take a long argument for current time and an int for converting to other time units but the current implementation is more efficient (I could also get rid of fetchCurrentTime() and make the getElaspsed...() methods take those parameters but again, no need for that).

 

Perhaps I misunderstood, if I did feel free to post some pseudocode tongue.png

ya you miss understood me a little, but i understand where your coming from. You have the abstract class there as the basic timer. Then you have the other classes extending the basic class so you can easily convert the timer into mili second and nano seconds and stuff. Personally i would just keep one class. Have the constructor with an argument. Which allow the user to set the current time (in mil secs ofc).

 

All I need is this basic class. (i posted the code for the noobies, trust me your code might seem a bit confusing to them at first.) 

public class Timer {

	private long period;
	private long start;
	
	public Timer(long period)	{
		this.period = period;
		this.start = System.currentTimeMillis();
	}

	public long getElapsed()	{
		return System.currentTimeMillis() - this.start;
	}
	
	public long getRemaining()	{
		return this.period - this.getElapsed();
	}
	
	public boolean isRunning()	{
		return this.getElapsed() <= this.period;
	}
	
	public void reset()	{
		this.start = System.currentTimeMillis();
	}
	
	public void stop()	{
		this.period = 0;
	}
	
	public static String format(long milliSeconds)	{
		long secs = milliSeconds / 1000L;
		return String.format("%02d:%02d:%02d", new Object[]	{
				Long.valueOf(secs / 3600L), Long.valueOf(secs % 3600L/ 60L), Long.valueOf(secs % 60L)
		});
	}
}
Edited by josedpay
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...