summaryrefslogtreecommitdiffstats
path: root/src/jake2/sys
diff options
context:
space:
mode:
authorHolger Zickner <[email protected]>2005-07-01 14:11:01 +0000
committerHolger Zickner <[email protected]>2005-07-01 14:11:01 +0000
commit667a27ca1186506757adae0997089f018beac49a (patch)
treed6c78403003446f5c1cc9eb0d81769cd4261182a /src/jake2/sys
parente4c1237ba261da7d3dcc00f474ab1428b9643317 (diff)
different Timers implementing Milliseconds()
Diffstat (limited to 'src/jake2/sys')
-rw-r--r--src/jake2/sys/HighPrecisionTimer.java32
-rw-r--r--src/jake2/sys/NanoTimer.java27
-rw-r--r--src/jake2/sys/StandardTimer.java27
-rw-r--r--src/jake2/sys/Timer.java35
4 files changed, 121 insertions, 0 deletions
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());
+ }
+}