// 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 Objects {
    public static void main(String[] args) {
        int[] itest = { 10000000, 100000000, 1000000000 };
        for(int n : itest) {
           (new TestIntegerObject(n)).test();
           (new TestIntegerSimple(n)).test();
        }
        int[] stestfixedsize = { 1000000, 10000000, 100000000 }; 
        for(int n : stestfixedsize) {
            (new TestStringFixedSize(n)).test();
            (new TestStringBuilderFixedSize(n)).test();
        }
        int[] stestgrowing = { 100000, 1000000 }; 
        for(int n : stestgrowing) {
            (new TestStringGrowing(n)).test();
            (new TestStringBuilderGrowing(n)).test();
        }
    }
}

abstract class Test {
    private String name;
    protected int n;
    protected Object res;
    public Test(String name, int n) {
        this.name = name;
        this.n = n;
    }
    public void test() {
        long t1 = System.currentTimeMillis();
        runtest();
        long t2 = System.currentTimeMillis();
        System.out.println(n + " " + name + " : " + (t2 - t1));
    }
    public abstract void runtest();
}

class TestIntegerObject extends Test {
    public TestIntegerObject(int n) {
        super("Integer", n);
    }
    public void runtest() {
        Integer v = new Integer(0);
        for(int i = 0; i < n; i++) {
            v = v + 1;
        }
        res = v;
    }
}

class TestIntegerSimple extends Test {
    public TestIntegerSimple(int n) {
        super("int", n);
    }
    public void runtest() {
        int v = 0;
        for(int i = 0; i < n; i++) {
            v = v + 1;
        }
        res = v;
    }
}

class TestStringFixedSize extends Test {
    public TestStringFixedSize(int n) {
        super("String fixed size", n);
    }
    public void runtest() {
        String v = "*";
        for(int i = 0; i < n; i++) {
            if(i % 2 == 0) {
                v = v + "*";
            } else {
                v = v.substring(0, 1);
            }
        }
        res = v;
    }
}
class TestStringBuilderFixedSize extends Test {
    public TestStringBuilderFixedSize(int n) {
        super("StringBuilder fixed size", n);
    }
    public void runtest() {
        StringBuilder v = new StringBuilder("*");
        for(int i = 0; i < n; i++) {
            if(i % 2 == 0) {
                v.append("*");
            } else {
                v.delete(1, 2);
            }
        }
        res = v;
    }
}
class TestStringGrowing extends Test {
    public TestStringGrowing(int n) {
        super("String growing", n);
    }
    public void runtest() {
        String v = "*";
        for(int i = 0; i < n; i++) {
            v = v + "*";
            if(i % (n/100) == 0) {
                v = "*";
            }
        }
        res = v;
    }
}

class TestStringBuilderGrowing extends Test {
    public TestStringBuilderGrowing(int n) {
        super("StringBuilder growing", n);
    }
    public void runtest() {
        StringBuilder v = new StringBuilder("*");
        for(int i = 0; i < n; i++) {
            v.append("*");
            if(i % (n/100) == 0) {
                v = new StringBuilder("*");
            }
        }
        res = v;
    }
}
