use of ch.obermuhlner.math.big.example.StopWatch in project big-math by eobermuhlner.
the class PerformanceBigDecimalMath method performanceReportOverPrecision.
@SafeVarargs
private static void performanceReportOverPrecision(PrintWriter writer, BigDecimal value, int precisionStart, int precisionEnd, int precisionStep, int repeats, List<String> functionNames, BiFunction<BigDecimal, MathContext, BigDecimal>... functions) {
// warmup
for (int i = 0; i < 1000; i++) {
for (BiFunction<BigDecimal, MathContext, BigDecimal> calculation : functions) {
try {
calculation.apply(value, MathContext.DECIMAL32);
} catch (ArithmeticException ex) {
// ignore
}
}
}
// print headers
writer.printf("%8s", "precision");
for (int fIndex = 0; fIndex < functionNames.size(); fIndex++) {
writer.print(",");
writer.printf("%8s", functionNames.get(fIndex));
}
writer.println();
// print types
writer.printf("%8s", "number");
for (int fIndex = 0; fIndex < functionNames.size(); fIndex++) {
writer.print(",");
writer.printf("%8s", "number");
}
writer.println();
// prepare data storage
int pCount = 0;
for (int precision = precisionStart; precision < precisionEnd; precision += precisionStep) {
pCount++;
}
long[][][] nanosFunctionPrecisionRepeat = new long[functions.length][][];
for (int fIndex = 0; fIndex < functions.length; fIndex++) {
nanosFunctionPrecisionRepeat[fIndex] = new long[pCount][];
for (int pIndex = 0; pIndex < pCount; pIndex++) {
nanosFunctionPrecisionRepeat[fIndex][pIndex] = new long[repeats];
}
}
// real measurement
for (int rIndex = 0; rIndex < repeats; rIndex++) {
for (int fIndex = 0; fIndex < functions.length; fIndex++) {
int pIndex = 0;
for (int precision = precisionStart; precision < precisionEnd; precision += precisionStep) {
MathContext mathContext = new MathContext(precision);
BiFunction<BigDecimal, MathContext, BigDecimal> calculation = functions[fIndex];
try {
StopWatch stopWatch = new StopWatch();
calculation.apply(value, mathContext);
nanosFunctionPrecisionRepeat[fIndex][pIndex][rIndex] = stopWatch.getElapsedNanos();
} catch (ArithmeticException ex) {
// ignore
}
pIndex++;
}
}
System.out.print(".");
}
System.out.println();
// write report
{
int p = 0;
for (int precision = precisionStart; precision < precisionEnd; precision += precisionStep) {
writer.printf("%8d", precision);
for (int fIndex = 0; fIndex < functions.length; fIndex++) {
writer.print(",");
long elapsedNanos = median(nanosFunctionPrecisionRepeat[fIndex][p]);
writer.printf("%8d", elapsedNanos);
}
p++;
writer.println();
}
}
}
use of ch.obermuhlner.math.big.example.StopWatch in project big-math by eobermuhlner.
the class PerformanceBigDecimalMath method performanceReportOverValue.
@SafeVarargs
private static void performanceReportOverValue(PrintWriter writer, MathContext mathContext, BigDecimal xStart, BigDecimal xEnd, BigDecimal xStep, int repeats, List<String> functionNames, BiFunction<BigDecimal, MathContext, BigDecimal>... functions) {
if (functionNames.size() != functions.length) {
throw new IllegalArgumentException("Must be same number of functionNames (" + functionNames.size() + ") and functions (" + functions.length + ")");
}
// warmup
for (BigDecimal x = xStart; x.compareTo(xEnd) <= 0; x = x.add(xStep)) {
for (BiFunction<BigDecimal, MathContext, BigDecimal> calculation : functions) {
try {
calculation.apply(x, MathContext.DECIMAL32);
} catch (ArithmeticException ex) {
// ignore
}
}
}
// print headers
writer.printf("%8s", "x");
for (int i = 0; i < functionNames.size(); i++) {
writer.print(",");
writer.printf("%8s", functionNames.get(i));
}
writer.println();
// print types
writer.printf("%8s", "number");
for (int i = 0; i < functionNames.size(); i++) {
writer.print(",");
writer.printf("%8s", "number");
}
writer.println();
// prepare data storage
int xCount = 0;
for (BigDecimal x = xStart; x.compareTo(xEnd) <= 0; x = x.add(xStep)) {
xCount++;
}
long[][][] nanosFunctionValueRepeat = new long[functions.length][][];
for (int fIndex = 0; fIndex < functions.length; fIndex++) {
nanosFunctionValueRepeat[fIndex] = new long[xCount][];
for (int xIndex = 0; xIndex < xCount; xIndex++) {
nanosFunctionValueRepeat[fIndex][xIndex] = new long[repeats];
}
}
// real measurements
for (int rIndex = 0; rIndex < repeats; rIndex++) {
int xIndex = 0;
for (BigDecimal x = xStart; x.compareTo(xEnd) <= 0; x = x.add(xStep)) {
for (int fIndex = 0; fIndex < functions.length; fIndex++) {
BiFunction<BigDecimal, MathContext, BigDecimal> function = functions[fIndex];
try {
StopWatch stopWatch = new StopWatch();
function.apply(x, mathContext);
nanosFunctionValueRepeat[fIndex][xIndex][rIndex] = stopWatch.getElapsedNanos();
} catch (ArithmeticException ex) {
// ignore
}
}
xIndex++;
}
System.out.print(".");
}
System.out.println();
// write report
{
int xIndex = 0;
for (BigDecimal x = xStart; x.compareTo(xEnd) <= 0; x = x.add(xStep)) {
writer.printf("%8.3f", x);
for (int fIndex = 0; fIndex < functions.length; fIndex++) {
writer.print(",");
long elapsedNanos = median(nanosFunctionValueRepeat[fIndex][xIndex]);
writer.printf("%8d", elapsedNanos);
}
writer.println();
xIndex++;
}
}
}
use of ch.obermuhlner.math.big.example.StopWatch in project big-math by eobermuhlner.
the class PerformanceBigDecimalMath method performanceReportAtan2_yx_neg10_to_10.
private static void performanceReportAtan2_yx_neg10_to_10() {
MathContext mathContext = REF_MATHCONTEXT;
BigDecimal xStart = BigDecimal.valueOf(-10);
BigDecimal xEnd = BigDecimal.valueOf(10);
BigDecimal xStep = BigDecimal.valueOf(2.0);
BigDecimal xStepWarmup = BigDecimal.valueOf(2.0);
BigDecimal yStart = BigDecimal.valueOf(-10);
BigDecimal yEnd = BigDecimal.valueOf(10);
BigDecimal yStep = BigDecimal.valueOf(2.0);
BigDecimal yStepWarmup = BigDecimal.valueOf(2.0);
int repeats = 3;
String name = "perf_atan2_yx_from_-10_to_10.csv";
// warmup
for (BigDecimal y = yStart; y.compareTo(yEnd) < 0; y = y.add(yStepWarmup)) {
for (BigDecimal x = xStart; x.compareTo(xEnd) < 0; x = x.add(xStepWarmup)) {
try {
BigDecimalMath.atan2(y, x, mathContext).doubleValue();
} catch (Exception ex) {
// ignore
}
}
}
try (PrintWriter writer = new PrintWriter(new FileWriter(OUTPUT_DIRECTORY + name))) {
for (BigDecimal x = xStart; x.compareTo(xEnd) <= 0; x = x.add(xStep)) {
writer.print(", ");
writer.print(x);
}
writer.println();
for (BigDecimal y = yStart; y.compareTo(yEnd) <= 0; y = y.add(yStep)) {
writer.print(y);
writer.print(", ");
for (BigDecimal x = xStart; x.compareTo(xEnd) <= 0; x = x.add(xStep)) {
if (!x.equals(xStart)) {
writer.print(", ");
}
System.out.println("x = " + x + " y = " + y);
double result = Double.MAX_VALUE;
try {
for (int i = 0; i < repeats; i++) {
StopWatch stopWatch = new StopWatch();
BigDecimalMath.atan2(y, x, mathContext).doubleValue();
result = Math.min(result, stopWatch.getElapsedMillis());
}
} catch (Exception ex) {
result = 0;
}
writer.print(result);
}
writer.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
use of ch.obermuhlner.math.big.example.StopWatch in project big-math by eobermuhlner.
the class PerformanceRegressionBigDecimalMath method measurePerformance.
private static void measurePerformance(String name, MathContext mathContext, Runnable function) {
int warmup = 1000;
for (int i = 0; i < warmup; i++) {
function.run();
}
int count = 1000;
long totalNanos = 0;
long minNanos = Long.MAX_VALUE;
long maxNanos = Long.MIN_VALUE;
List<Long> allNanos = new ArrayList<>();
for (int i = 0; i < count; i++) {
StopWatch stopWatch = new StopWatch();
function.run();
long nanos = stopWatch.getElapsedNanos();
totalNanos += nanos;
minNanos = Math.min(minNanos, nanos);
maxNanos = Math.max(maxNanos, nanos);
allNanos.add(nanos);
}
long avgNanos = totalNanos / count;
long medianNanos = median(allNanos);
System.out.printf("%-20s, %15d, %15d, %15d, %15d\n", name, avgNanos, medianNanos, minNanos, maxNanos);
}
Aggregations