// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package test;

import java.util.Random;

public class PerfTestManual {
    private static Random rng = new Random();
    private static void testBuiltin(int rep) {
        long t1 = System.currentTimeMillis();
        for(int j = 0; j < rep; j++) {
            double a = rng.nextDouble() * 1000.0;
            double sqrta = Work.sqrtBuiltin(a);
            if(Math.abs(a - sqrta * sqrta) > 0.0000001) throw new RuntimeException("Ooops");
        }
        long t2 = System.currentTimeMillis();
        System.out.printf("Builtin : %.1f ns\n", (t2 - t1) * 1000000.0 / rep);
    }
    private static void testNewton(int rep) {
        long t1 = System.currentTimeMillis();
        for(int j = 0; j < rep; j++) {
            double a = rng.nextDouble() * 1000.0;
            double sqrta = Work.sqrtNewton(a);
            if(Math.abs(a - sqrta * sqrta) > 0.0000001) throw new RuntimeException("Ooops");
        }
        long t2 = System.currentTimeMillis();
        System.out.printf("Newton : %.1f ns\n", (t2 - t1) * 1000000.0 / rep);
    }
    public static void main(String[] args) {
        for(int k = 0; k < 5; k++) {
            testBuiltin(10000000);
        }
        for(int k = 0; k < 5; k++) {
            testNewton(10000000);
        }
    }
}
