// 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 StrLoop {
    public static String test1x(int n) {
        String res = "";
        for(int i = 0; i < n; i++) {
            res = res + "ABC";
        }
        return res;
    }
    public static String test2x(int n) {
        StringBuilder res = new StringBuilder();
        for(int i = 0; i < n; i++) {
            res.append("ABC");
        }
        return res.toString();
    }
    public static double test1(int n) {
        int rep = Math.max(100000/n, 1);
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < rep; i++) {
            if(test1x(n).length() != n*3) {
                throw new RuntimeException("Ooops");
            }
        }
        long t2 = System.currentTimeMillis();
        return (t2 - t1)/1000.0/rep;
    }
    public static double test2(int n) {
        int rep = Math.max(100*100000/n, 1);
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < rep; i++) {
            if(test2x(n).length() != n*3) {
                throw new RuntimeException("Ooops");
            }
        }
        long t2 = System.currentTimeMillis();
        return (t2 - t1)/1000.0/rep;
    }
    public static void test(int n) {
        System.out.printf("%9d %10.6f %10.6f\n", n, test1(n), test2(n));
    }
    public static void main(String[] args) {
        int n = 1;
        for(int i = 0; i < 5; i++) {
            n *= 10;
            test(n);
        }
    }
}
