// 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) 
import java.math.BigDecimal;

public class BDSpeedMT2 {
    private static final int N1 = 10_000;
    private static final int N2 = 1_000_000;
    private static class Worker extends Thread {
        private int nt;
        public Worker(int nt) {
            this.nt = nt;
        }
        public void run() {
            BigDecimal v = new BigDecimal("123.500000");
            for(int j = 0; j < N1 / nt; j++) {
                BigDecimal sum = new BigDecimal(0);
                for(int i = 0; i < N2; i++) {
                    sum = sum.add(v);
                }
            }
        }
    }
    private static void test(int nt) throws InterruptedException {
        long t1 = System.currentTimeMillis();
        Worker[] t = new Worker[nt];
        for(int i = 0; i < nt; i++) {
            t[i] = new Worker(nt);
        }
        for(int i = 0; i < nt; i++) {
            t[i].start();
        }
        for(int i = 0; i < nt; i++) {
            t[i].join();
        }
        long t2 = System.currentTimeMillis();
        System.out.printf("%d threads : %.1f seconds\n", nt, (t2 - t1) / 1000.0);
    }
    public static void main(String[] args) throws Exception {
        int nt = 1;
        while(nt < 100) {
            test(nt);
            nt *= 2;
        }
    }
}
