Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dwclark committed Aug 27, 2016
0 parents commit 64465f1
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*~
*.class
28 changes: 28 additions & 0 deletions 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();
}
75 changes: 75 additions & 0 deletions 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;
}
}
3 changes: 3 additions & 0 deletions RunnableState.groovy
@@ -0,0 +1,3 @@
public interface RunnableState extends Runnable {
public Object getState();
}
100 changes: 100 additions & 0 deletions 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);
}
}
9 changes: 9 additions & 0 deletions 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)

0 comments on commit 64465f1

Please sign in to comment.