use of uk.ac.sussex.gdsc.test.utils.TimingResult in project GDSC-SMLM by aherbert.
the class ErfGaussian2DFunctionTest method functionIsFasterUsingForEach.
// Speed test forEach verses equivalent eval() function calls
@SpeedTag
@Test
void functionIsFasterUsingForEach() {
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
final ErfGaussian2DFunction f1 = (ErfGaussian2DFunction) this.f1;
final LocalList<double[]> params = new LocalList<>();
for (final double background : testbackground) {
// Peak 1
for (final double signal1 : testsignal1) {
for (final double cx1 : testcx1) {
for (final double cy1 : testcy1) {
for (final double cz1 : testcz1) {
for (final double[] w1 : testw1) {
for (final double angle1 : testangle1) {
final double[] a = createParameters(background, signal1, cx1, cy1, cz1, w1[0], w1[1], angle1);
params.add(a);
}
}
}
}
}
}
}
final double[][] x = params.toArray(new double[0][]);
final int runs = 10000 / x.length;
final TimingService ts = new TimingService(runs);
ts.execute(new FunctionTimingTask(f1, x, 2));
ts.execute(new FunctionTimingTask(f1, x, 1));
ts.execute(new FunctionTimingTask(f1, x, 0));
ts.execute(new ForEachTimingTask(f1, x, 2));
ts.execute(new ForEachTimingTask(f1, x, 1));
ts.execute(new ForEachTimingTask(f1, x, 0));
final int size = ts.getSize();
ts.repeat(size);
if (logger.isLoggable(Level.INFO)) {
logger.info(ts.getReport());
}
for (int i = 1; i <= 3; i++) {
final TimingResult slow = ts.get(-i - 3);
final TimingResult fast = ts.get(-i);
logger.log(TestLogUtils.getTimingRecord(slow, fast));
}
}
use of uk.ac.sussex.gdsc.test.utils.TimingResult in project GDSC-SMLM by aherbert.
the class ErfGaussian2DFunctionTest method functionIsFasterThanEquivalentGaussian2DFunction.
// Speed test verses equivalent Gaussian2DFunction
@SpeedTag
@Test
void functionIsFasterThanEquivalentGaussian2DFunction() {
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
final int flags = this.flags & ~GaussianFunctionFactory.FIT_ERF;
final Gaussian2DFunction gf = GaussianFunctionFactory.create2D(1, maxx, maxy, flags, zModel);
final boolean zDepth = (flags & GaussianFunctionFactory.FIT_Z) != 0;
final LocalList<double[]> params1 = new LocalList<>();
final LocalList<double[]> params2 = new LocalList<>();
for (final double background : testbackground) {
// Peak 1
for (final double signal1 : testsignal1) {
for (final double cx1 : testcx1) {
for (final double cy1 : testcy1) {
for (final double cz1 : testcz1) {
for (final double[] w1 : testw1) {
for (final double angle1 : testangle1) {
double[] params = createParameters(background, signal1, cx1, cy1, cz1, w1[0], w1[1], angle1);
params1.add(params);
if (zDepth) {
// Change to a standard free circular function
params = params.clone();
params[Gaussian2DFunction.X_SD] *= zModel.getSx(params[Gaussian2DFunction.Z_POSITION]);
params[Gaussian2DFunction.Y_SD] *= zModel.getSy(params[Gaussian2DFunction.Z_POSITION]);
params[Gaussian2DFunction.Z_POSITION] = 0;
params2.add(params);
}
}
}
}
}
}
}
}
final double[][] x = params1.toArray(new double[0][]);
final double[][] x2 = (zDepth) ? params2.toArray(new double[0][]) : x;
final int runs = 10000 / x.length;
final TimingService ts = new TimingService(runs);
ts.execute(new FunctionTimingTask(gf, x2, 1));
ts.execute(new FunctionTimingTask(gf, x2, 0));
ts.execute(new FunctionTimingTask(f1, x, 2));
ts.execute(new FunctionTimingTask(f1, x, 1));
ts.execute(new FunctionTimingTask(f1, x, 0));
final int size = ts.getSize();
ts.repeat(size);
if (logger.isLoggable(Level.INFO)) {
logger.info(ts.getReport());
}
for (int i = 1; i <= 2; i++) {
final TimingResult slow = ts.get(-i - 3);
final TimingResult fast = ts.get(-i);
logger.log(TestLogUtils.getTimingRecord(slow, fast));
}
}
use of uk.ac.sussex.gdsc.test.utils.TimingResult in project GDSC-SMLM by aherbert.
the class LvmGradientProcedureTest method gradientProcedureIsFasterUnrolledThanGradientProcedure.
private void gradientProcedureIsFasterUnrolledThanGradientProcedure(RandomSeed seed, final int nparams, final Type type, final boolean precomputed) {
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
final int iter = 100;
final ArrayList<double[]> paramsList = new ArrayList<>(iter);
final ArrayList<double[]> yList = new ArrayList<>(iter);
createData(RngUtils.create(seed.getSeed()), 1, iter, paramsList, yList);
// Remove the timing of the function call by creating a dummy function
final FakeGradientFunction fgf = new FakeGradientFunction(blockWidth, nparams);
final Gradient1Function func;
if (precomputed) {
final double[] b = SimpleArrayUtils.newArray(fgf.size(), 0.1, 1.3);
func = OffsetGradient1Function.wrapGradient1Function(fgf, b);
} else {
func = fgf;
}
final FastLog fastLog = type == Type.FAST_LOG_MLE ? getFastLog() : null;
final IntArrayFormatSupplier msgA = new IntArrayFormatSupplier("A [%d]", 1);
final IntArrayFormatSupplier msgB = new IntArrayFormatSupplier("B [%d]", 1);
for (int i = 0; i < paramsList.size(); i++) {
final LvmGradientProcedure p1 = createProcedure(type, yList.get(i), func, fastLog);
p1.gradient(paramsList.get(i));
p1.gradient(paramsList.get(i));
final LvmGradientProcedure p2 = LvmGradientProcedureUtils.create(yList.get(i), func, type, fastLog);
p2.gradient(paramsList.get(i));
p2.gradient(paramsList.get(i));
// Check they are the same
Assertions.assertArrayEquals(p1.getAlphaLinear(), p2.getAlphaLinear(), msgA.set(0, i));
Assertions.assertArrayEquals(p1.beta, p2.beta, msgB.set(0, i));
}
// Realistic loops for an optimisation
final int loops = 15;
// Run till stable timing
final Timer t1 = new Timer() {
@Override
void run() {
for (int i = 0, k = 0; i < paramsList.size(); i++) {
final LvmGradientProcedure p1 = createProcedure(type, yList.get(i), func, fastLog);
for (int j = loops; j-- > 0; ) {
p1.gradient(paramsList.get(k++ % iter));
}
}
}
};
final long time1 = t1.getTime();
final Timer t2 = new Timer(t1.loops) {
@Override
void run() {
for (int i = 0, k = 0; i < paramsList.size(); i++) {
final LvmGradientProcedure p2 = LvmGradientProcedureUtils.create(yList.get(i), func, type, fastLog);
for (int j = loops; j-- > 0; ) {
p2.gradient(paramsList.get(k++ % iter));
}
}
}
};
final long time2 = t2.getTime();
logger.log(TestLogUtils.getTimingRecord(new TimingResult(() -> String.format("%s, Precomputed=%b : Standard", type, precomputed), time1), new TimingResult(() -> String.format("Unrolled %d", nparams), time2)));
}
use of uk.ac.sussex.gdsc.test.utils.TimingResult in project GDSC-SMLM by aherbert.
the class FastMathTest method cbrtIsFaster.
// @formatter:on
@SpeedTag
@Test
void cbrtIsFaster() {
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
// Q. What is a suitable range for this test?
final int range = 5;
final int steps = 10000;
final double[] x = new double[steps];
final double total = 2 * range;
final double step = total / steps;
for (int i = 0; i < steps; i++) {
x[i] = -range + i * step;
}
final TimingService ts = new TimingService(5);
ts.execute(new MathPow1_3(x));
ts.execute(new FastMathPow1_3(x));
ts.execute(new MathCbrt(x));
ts.execute(new FastMathCbrt(x));
final int size = ts.getSize();
ts.repeat(size);
if (logger.isLoggable(Level.INFO)) {
logger.info(ts.getReport());
}
final TimingResult fast = ts.get(-1);
for (int k = 2; k <= 3; k++) {
final TimingResult slow = ts.get(-k);
logger.log(TestLogUtils.getTimingRecord(slow, fast));
}
}
use of uk.ac.sussex.gdsc.test.utils.TimingResult in project GDSC-SMLM by aherbert.
the class GaussianFilterTest method floatFilterIsFasterThanDoubleFilter.
@SpeedTag
@SeededTest
void floatFilterIsFasterThanDoubleFilter(RandomSeed seed) {
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
final float[][] data = new float[10][];
for (int i = 0; i < data.length; i++) {
data[i] = createData(rg, size, size);
}
final TimingService ts = new TimingService();
for (final double sigma : sigmas) {
ts.execute(new MyTimingTask(new FloatFilter(false), data, sigma));
ts.execute(new MyTimingTask(new DpFilter(false), data, sigma));
ts.execute(new MyTimingTask(new DoubleFilter(false), data, sigma));
}
final int size = ts.getSize();
ts.repeat();
if (logger.isLoggable(Level.INFO)) {
logger.info(ts.getReport(size));
}
final int n = size / sigmas.length;
for (int i = 0, j = size; i < sigmas.length; i++, j += n) {
for (int k = 1; k < n; k++) {
final TimingResult slow = ts.get(j + k);
final TimingResult fast = ts.get(j);
logger.log(TestLogUtils.getTimingRecord(slow, fast));
}
}
}
Aggregations