// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class BigOTester {
    public interface Algorithm {
        public int t(int n);
    }
    private static long time(Algorithm alg, int n, int rep) {
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < rep; i++) {
            alg.t(n);
        }
        long t2 = System.currentTimeMillis();
        return t2 - t1;
    }
    private final static int SECS = 5;
    private static double time(Algorithm alg, int n) {
        int repprsec = 1;
        while(time(alg, n, repprsec) < 1000) repprsec *= 2;
        int rep = SECS * repprsec;
        return time(alg, n, rep) / (double)rep;
    }
    private final static double MAXDEV = 0.05;
    private final static boolean match(double tlow, double thigh, double clow, double chigh) {
        double ratio = (thigh / tlow) / (chigh / clow);
        return (1 - MAXDEV) < ratio && (ratio < 1 + MAXDEV);
    }
    private final static int LOW = 1000;
    private final static int HIGH = 10000;
    public static void test(Algorithm alg) {
        double tlow = time(alg, LOW);
        double thigh = time(alg, HIGH);
        if(match(tlow, thigh, 1.0, 1.0)) {
            System.out.println("O(1)");
        } else if(match(tlow, thigh, Math.log(LOW), Math.log(HIGH))) {
            System.out.println("O(logn)");
        } else if(match(tlow, thigh, (double)LOW, (double)HIGH)) {
            System.out.println("O(n)");
        } else if(match(tlow, thigh, (double)LOW * Math.log(LOW), (double)HIGH * Math.log(HIGH))) {
            System.out.println("O(nlogn)");
        } else if(match(tlow, thigh, (double)LOW * (double)LOW, (double)HIGH * (double)HIGH)) {
            System.out.println("O(n^2)");
        } else if(match(tlow, thigh, Math.pow(LOW, 3), Math.pow(HIGH, 3))) {
            System.out.println("O(n^3)");
        } else {
            System.out.println("Unknown big O");
        }
    }
    public static void main(String[] args) {
        test(new Algorithm() {
            public int t(int n) {
                int result = 0;
                for(int i = 0; i < n ; i++)
                {
                    result = result + i;
                } 
                return result;
            }
        });
        test(new Algorithm() {
            public int t(int n) {
                int result = 0;
                for(int i = 0; i < n ; i += 2)
                {
                    result = result + i;
                } 
                return result;
            }
        });
        test(new Algorithm() {
            public int t(int n)
            {
                int result = 0;
                for (int i = 0; i < n; i++)
                {
                    for (int j = 1; j < n; j *= 2)
                    {
                        result++;
                    }
                }
                return result;
            }
        });
    }
}
