<commit_before>package ch.obermuhlner.math.big.example;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.function.BiFunction;

import ch.obermuhlner.math.big.BigDecimalMath;

public class PerformanceBigDecimalMath {

	public static void main(String[] args) {
		MathContext mathContext = new MathContext(200);
		
		performanceReportOverPrecision(
				(x, calculationMathContext) -> BigDecimalMath.log(x, calculationMathContext),
				BigDecimal.valueOf(3.1),
				10,
				1000,
				10);

//		performanceReportOverValue(
//				(x, calculationMathContext) -> BigDecimalMath.log(x, calculationMathContext),
//				mathContext,
//				+0.01,
//				+2.0,
//				+0.01);
		
//		performanceReportOverValue(
//				(x, calculationMathContext) -> BigDecimalMath.log(x, calculationMathContext),
//				mathContext,
//				+0.5,
//				+100.0,
//				+0.5);
	}

	private static void performanceReportOverValue(BiFunction<BigDecimal, MathContext, BigDecimal> calculation, MathContext mathContext, double xStart, double xEnd, double xStep) {
		performanceReportOverValue(calculation, mathContext, BigDecimal.valueOf(xStart), BigDecimal.valueOf(xEnd), BigDecimal.valueOf(xStep));
	}

	private static void performanceReportOverValue(BiFunction<BigDecimal, MathContext, BigDecimal> calculation, MathContext mathContext, BigDecimal xStart, BigDecimal xEnd, BigDecimal xStep) {
		// warmup
		for (BigDecimal x = xStart; x.compareTo(xEnd) <= 0; x = x.add(xStep)) {
			calculation.apply(x, MathContext.DECIMAL32);
		}
		
		// real measurement
		for (BigDecimal x = xStart; x.compareTo(xEnd) <= 0; x = x.add(xStep)) {
			StopWatch stopWatch = new StopWatch();
			
			calculation.apply(x, mathContext);
			long elapsedMillis = stopWatch.getElapsedMillis();
			
			System.out.println(x + "," + elapsedMillis);
		}
	}

	private static void performanceReportOverPrecision(BiFunction<BigDecimal, MathContext, BigDecimal> calculation, BigDecimal value, int precisionStart, int precisionEnd, int precisionStep) {
		// warmup
		for (int i = 0; i < 1000; i++) {
			calculation.apply(value, MathContext.DECIMAL32);
		}
		
		// real measurement
		for (int precision = precisionStart; precision < precisionEnd; precision+=precisionStep) {
			MathContext mathContext = new MathContext(precision);
			
			StopWatch stopWatch = new StopWatch();
			
			calculation.apply(value, mathContext);
			long elapsedMillis = stopWatch.getElapsedMillis();
			
			System.out.println(precision + "," + elapsedMillis);
		}
	}
}<commit_msg>improved performance reports<commit_after>package ch.obermuhlner.math.big.example;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.function.BiFunction;

import ch.obermuhlner.math.big.BigDecimalMath;

public class PerformanceBigDecimalMath {

	public static void main(String[] args) {

		performanceReportStandardFunctions();
		//performanceReportSlowFunctions();

		//performanceReportLogBigRange();

		//performanceReportOverPrecision();
	}

	private static void performanceReportStandardFunctions() {
		MathContext mathContext = new MathContext(1000);

		printHeaders("x", "exp", "sqrt", "root2", "root3", "sin", "cos");
		performanceReportOverValue(
				mathContext,
				+0,
				+2.0,
				+0.01,
				(x, calculationMathContext) -> BigDecimalMath.exp(x, calculationMathContext),
				(x, calculationMathContext) -> BigDecimalMath.sqrt(x, calculationMathContext),
				(x, calculationMathContext) -> BigDecimalMath.root(new BigDecimal(2), x, calculationMathContext),
				(x, calculationMathContext) -> BigDecimalMath.root(new BigDecimal(3), x, calculationMathContext),
				(x, calculationMathContext) -> BigDecimalMath.sin(x, calculationMathContext),
				(x, calculationMathContext) -> BigDecimalMath.cos(x, calculationMathContext));
	}

	private static void performanceReportSlowFunctions() {
		MathContext mathContext = new MathContext(200);

		printHeaders("x", "log", "exp", "pow");
		performanceReportOverValue(
				mathContext,
				+0.01,
				+2.0,
				+0.01,
				(x, calculationMathContext) -> BigDecimalMath.exp(x, calculationMathContext),
				(x, calculationMathContext) -> BigDecimalMath.log(x, calculationMathContext),
				(x, calculationMathContext) -> BigDecimalMath.pow(BigDecimal.valueOf(123.456), x, calculationMathContext));
	}

	private static void performanceReportLogBigRange() {
		MathContext mathContext = new MathContext(300);

		printHeaders("x", "log");
		performanceReportOverValue(
				mathContext,
				+1,
				+100,
				+1,
				(x, calculationMathContext) -> BigDecimalMath.log(x, calculationMathContext));
	}

	private static void performanceReportOverPrecision() {
		printHeaders("precision", "exp", "log", "pow");
		performanceReportOverPrecision(
				BigDecimal.valueOf(3.1),
				10,
				1000,
				10,
				(x, calculationMathContext) -> BigDecimalMath.exp(x, calculationMathContext),
				(x, calculationMathContext) -> BigDecimalMath.log(x, calculationMathContext),
				(x, calculationMathContext) -> BigDecimalMath.pow(BigDecimal.valueOf(123.456), x, calculationMathContext));
	}

	private static void printHeaders(String... headers) {
		for (int i = 0; i < headers.length; i++) {
			if (i != 0) {
				System.out.print(",");
			}
			System.out.printf("%6s", headers[i]);
		}
		System.out.println();
	}

	@SafeVarargs
	private static void performanceReportOverValue(MathContext mathContext, double xStart, double xEnd, double xStep, BiFunction<BigDecimal, MathContext, BigDecimal>... calculations) {
		performanceReportOverValue(mathContext, BigDecimal.valueOf(xStart), BigDecimal.valueOf(xEnd), BigDecimal.valueOf(xStep), calculations);
	}

	private static void performanceReportOverValue(MathContext mathContext, BigDecimal xStart, BigDecimal xEnd, BigDecimal xStep, BiFunction<BigDecimal, MathContext, BigDecimal>... calculations) {
		// warmup
		for (BigDecimal x = xStart; x.compareTo(xEnd) <= 0; x = x.add(xStep)) {
			for (BiFunction<BigDecimal, MathContext, BigDecimal> calculation : calculations) {
				try {
					calculation.apply(x, MathContext.DECIMAL32);
				}
				catch (ArithmeticException ex) {
					// ignore
				}
			}
		}

		// real measurement
		for (BigDecimal x = xStart; x.compareTo(xEnd) <= 0; x = x.add(xStep)) {
			long[] elapsedMillis = new long[calculations.length];

			for (int i = 0; i < calculations.length; i++) {
				BiFunction<BigDecimal, MathContext, BigDecimal> calculation = calculations[i];

				StopWatch stopWatch = new StopWatch();
				try {
					calculation.apply(x, mathContext);
					elapsedMillis[i] = stopWatch.getElapsedMillis();
				}
				catch (ArithmeticException ex) {
					// ignore
				}

			}

			System.out.printf("%6.3f", x);
			for (int i = 0; i < elapsedMillis.length; i++) {
				System.out.print(",");
				System.out.printf("%6d", elapsedMillis[i]);
			}
			System.out.println();
		}
	}

	private static void performanceReportOverPrecision(BigDecimal value, int precisionStart, int precisionEnd, int precisionStep, BiFunction<BigDecimal, MathContext, BigDecimal>... calculations) {
		// warmup
		for (int i = 0; i < 1000; i++) {
			for (BiFunction<BigDecimal, MathContext, BigDecimal> calculation : calculations) {
				try {
					calculation.apply(value, MathContext.DECIMAL32);
				}
				catch (ArithmeticException ex) {
					// ignore
				}
			}
		}

		// real measurement
		for (int precision = precisionStart; precision < precisionEnd; precision += precisionStep) {
			long[] elapsedMillis = new long[calculations.length];
			MathContext mathContext = new MathContext(precision);

			for (int i = 0; i < calculations.length; i++) {
				BiFunction<BigDecimal, MathContext, BigDecimal> calculation = calculations[i];

				StopWatch stopWatch = new StopWatch();

				try {
					calculation.apply(value, mathContext);
					elapsedMillis[i] = stopWatch.getElapsedMillis();
				}
				catch (ArithmeticException ex) {
					// ignore
				}
			}

			System.out.printf("%9d", precision);
			for (int i = 0; i < elapsedMillis.length; i++) {
				System.out.print(",");
				System.out.printf("%6d", elapsedMillis[i]);
			}
			System.out.println();
		}
	}
}
{'fraction_non_alphanumeric': 0.12781768640079266, 'fraction_numerical': 0.014614812979935596, 'mean_word_length': 4.302035456336179, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '381955', 'n_tokens_mistral': 2933, 'n_tokens_neox': 2436, 'n_words': 533}