From 667a27ca1186506757adae0997089f018beac49a Mon Sep 17 00:00:00 2001 From: Holger Zickner Date: Fri, 1 Jul 2005 14:11:01 +0000 Subject: different Timers implementing Milliseconds() --- src/jake2/sys/HighPrecisionTimer.java | 32 ++++++++++++++++++++++++++++++++ src/jake2/sys/NanoTimer.java | 27 +++++++++++++++++++++++++++ src/jake2/sys/StandardTimer.java | 27 +++++++++++++++++++++++++++ src/jake2/sys/Timer.java | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 src/jake2/sys/HighPrecisionTimer.java create mode 100644 src/jake2/sys/NanoTimer.java create mode 100644 src/jake2/sys/StandardTimer.java create mode 100644 src/jake2/sys/Timer.java (limited to 'src/jake2') diff --git a/src/jake2/sys/HighPrecisionTimer.java b/src/jake2/sys/HighPrecisionTimer.java new file mode 100644 index 0000000..a8a0201 --- /dev/null +++ b/src/jake2/sys/HighPrecisionTimer.java @@ -0,0 +1,32 @@ +/* + * HighPrecisionTimer.java + * Copyright (C) 2005 + * + * $Id: HighPrecisionTimer.java,v 1.1 2005-07-01 14:11:00 hzi Exp $ + */ +package jake2.sys; + +import sun.misc.Perf; + + + + +class HighPrecisionTimer extends Timer { + + private Perf perf = Perf.getPerf(); + private double f = 1000.0 / perf.highResFrequency(); + private long base; + + HighPrecisionTimer() { + base = perf.highResCounter(); + } + + public long currentTimeMillis() { + long time = perf.highResCounter(); + long delta = time - base; + if (delta < 0) { + delta += Long.MAX_VALUE + 1; + } + return (long)(delta * f); + } +} diff --git a/src/jake2/sys/NanoTimer.java b/src/jake2/sys/NanoTimer.java new file mode 100644 index 0000000..d654fe8 --- /dev/null +++ b/src/jake2/sys/NanoTimer.java @@ -0,0 +1,27 @@ +/* + * NanoTimer.java + * Copyright (C) 2005 + * + * $Id: NanoTimer.java,v 1.1 2005-07-01 14:11:00 hzi Exp $ + */ +package jake2.sys; + + +public class NanoTimer extends Timer { + + private long base; + + NanoTimer() { + base = System.nanoTime(); + } + + public long currentTimeMillis() { + long time = System.nanoTime(); + long delta = time - base; + if (delta < 0) { + delta += Long.MAX_VALUE + 1; + } + return (long)(delta * 0.000001); + } + +} diff --git a/src/jake2/sys/StandardTimer.java b/src/jake2/sys/StandardTimer.java new file mode 100644 index 0000000..133bb24 --- /dev/null +++ b/src/jake2/sys/StandardTimer.java @@ -0,0 +1,27 @@ +/* + * StandardTimer.java + * Copyright (C) 2005 + * + * $Id: StandardTimer.java,v 1.1 2005-07-01 14:11:00 hzi Exp $ + */ +package jake2.sys; + + +class StandardTimer extends Timer { + + private long base; + + StandardTimer() { + base = System.currentTimeMillis(); + } + + public long currentTimeMillis() { + long time = System.currentTimeMillis(); + long delta = time - base; + if (delta < 0) { + delta += Long.MAX_VALUE + 1; + } + return delta; + } + +} diff --git a/src/jake2/sys/Timer.java b/src/jake2/sys/Timer.java new file mode 100644 index 0000000..4eede1f --- /dev/null +++ b/src/jake2/sys/Timer.java @@ -0,0 +1,35 @@ +/* + * Timer.java + * Copyright (C) 2005 + * + * $Id: Timer.java,v 1.1 2005-07-01 14:11:01 hzi Exp $ + */ +package jake2.sys; + +import jake2.Globals; + + +public abstract class Timer { + + abstract public long currentTimeMillis(); + private static long time = 0; + + static Timer t; + + static { + try { + t = new NanoTimer(); + } catch (Throwable e) { + try { + t = new HighPrecisionTimer(); + } catch (Throwable e1) { + t = new StandardTimer(); + } + } + System.out.println("using " + t.getClass().getName()); + } + + public static int Milliseconds() { + return Globals.curtime = (int)(t.currentTimeMillis()); + } +} -- cgit v1.2.3