Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 64465f1
Showing
6 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*~ | ||
*.class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public interface RunnableState extends Runnable { | ||
public Object getState(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |