diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c06bd3c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*~ +*.class diff --git a/Functions.groovy b/Functions.groovy new file mode 100644 index 0000000..47dc118 --- /dev/null +++ b/Functions.groovy @@ -0,0 +1,28 @@ +import java.util.function.DoubleUnaryOperator; +import groovy.transform.CompileStatic; + +class Functions { + + abstract static class One implements DoubleUnaryOperator { + @Override + public String toString() { + return "x^2"; + } + } + + final static class SlowOne extends One { + public double applyAsDouble(double x) { + return Math.pow(x, 2); + } + } + + @CompileStatic + final static class FastOne extends One { + public double applyAsDouble(double x) { + return Math.pow(x, 2); + } + } + + public static One slowOne = new SlowOne(); + public static One fastOne = new FastOne(); +} diff --git a/Integrate.groovy b/Integrate.groovy new file mode 100644 index 0000000..40469c5 --- /dev/null +++ b/Integrate.groovy @@ -0,0 +1,75 @@ +import java.util.function.DoubleUnaryOperator; +import groovy.transform.CompileStatic; + +class Integrate { + + final int steps; + final double lower; + final double upper; + + public Integrate(int steps, final double lower, final double upper) { + this.steps = steps; + this.lower = lower; + this.upper = upper; + } + + @CompileStatic + public double getDelta() { + return Math.abs(upper - lower) / (double) steps; + } + + public RunnableState slowRun(final DoubleUnaryOperator function) { + Integrate THIS = this; + + return new RunnableState() { + double _state; + public Object getState() { _state; } + public void run() { + _state = THIS.slow(function); + } + } + } + + public double slow(final DoubleUnaryOperator function) { + double d = delta; + int count = 0; + double total = 0.0d; + double xValue = lower; + + while(count < steps) { + total += (d * function.applyAsDouble(xValue)); + ++count; + xValue += d; + } + + return total; + } + + public RunnableState fastRun(final DoubleUnaryOperator function) { + Integrate THIS = this; + + return new RunnableState() { + double _state; + public Object getState() { _state; } + public void run() { + _state = THIS.fast(function); + } + } + } + + @CompileStatic + public double fast(final DoubleUnaryOperator function) { + double d = delta; + int count = 0; + double total = 0.0d; + double xValue = lower; + + while(count < steps) { + total += (d * function.applyAsDouble(xValue)); + ++count; + xValue += d; + } + + return total; + } +} diff --git a/RunnableState.groovy b/RunnableState.groovy new file mode 100644 index 0000000..f99d41a --- /dev/null +++ b/RunnableState.groovy @@ -0,0 +1,3 @@ +public interface RunnableState extends Runnable { + public Object getState(); +} diff --git a/TimeIt.groovy b/TimeIt.groovy new file mode 100644 index 0000000..4acaaab --- /dev/null +++ b/TimeIt.groovy @@ -0,0 +1,100 @@ +import groovy.transform.CompileStatic; +import java.math.RoundingMode; +import java.util.concurrent.TimeUnit; +import static java.util.concurrent.TimeUnit.*; + +class TimeIt { + + static class Info { + TimeUnit units = NANOSECONDS; + int ops; + long start; + long end; + + public Info(final int ops) { + this.ops = ops; + this.start = System.nanoTime(); + } + + public Info stop() { + this.end = System.nanoTime(); + return this; + } + + public long getTotal() { + return end - start; + } + + public long getSeconds() { + return units.toSeconds(total); + } + + public long getMilliSeconds() { + return units.toMillis(total); + } + + public String getDisplayUnits() { + return units.toString().toLowerCase(); + } + + public String getDisplayUnit() { + String tmp = displayUnits; + return tmp.substring(0, tmp.length() - 1); + } + + private BigDecimal scale(double val) { + BigDecimal bd = val; + return bd.setScale(2, RoundingMode.UP); + } + + @Override + public String toString() { + String ret = "Total Time: ${total} ${displayUnits}, ${scale(ops / total)} ops/${displayUnits}"; + + if(milliSeconds) { + ret += ", ${scale(ops / milliSeconds)} ops/ms" + } + + if(seconds) { + ret += ", ${scale(ops / seconds)} ops/sec"; + } + + return ret; + } + } + + public static Runnable warmUp(final Integer ops, final RunnableState r) { + ops.times { r.run(); } + println("Warm Up State: ${r.state}"); + return r; + } + + public static Runnable warmUp(final RunnableState r) { + return warmUp(15_000, r); + } + + @CompileStatic + public static Info time(final int warmUpCycles, final int total, final RunnableState r) { + warmUp(warmUpCycles, r); + int counter = 0; + Info info = new Info(total); + while(counter < total) { + ++counter; + r.run(); + } + + info.stop(); + println("State: ${r.state}"); + return info; + } + + @CompileStatic + public static Info time(final int total, final RunnableState r) { + return time(15_000, total, r); + } + + @CompileStatic + public static Info time(final RunnableState r) { + return time(15_000, 100_000, r); + } +} diff --git a/presentation.profile b/presentation.profile new file mode 100644 index 0000000..70195bc --- /dev/null +++ b/presentation.profile @@ -0,0 +1,9 @@ +:clear +:purge all +:load RunnableState.groovy +:load TimeIt.groovy +:load Integrate.groovy +:load Functions.groovy +:import static TimeIt.* +:import static Functions.* +sqIntegrate = new Integrate(1_000, 0.0d, 1.0d)