0

I have two implementations of singleton classes

public class Test2 {

private static Test2 _instance=new Test2();

private Test2(){

}

public static synchronized Test2 getInstance(){

    if(_instance == null){          
        _instance = new Test2();
    }
    return _instance;
}
}

And:

public class TestSingleton {

private static TestSingleton _instance=new TestSingleton();

private TestSingleton(){

}

public static TestSingleton getInstance(){
    if (_instance == null) {
        synchronized (TestSingleton.class) {

            if (_instance == null) {
        _instance = new TestSingleton();
            }
        }
    }
    return _instance;
}

I want to parametrize my finding in terms of time taken, what I did is this:

Callable<Long> task = new Callable<Long>() {
        @Override
        public Long call() throws Exception {
            long start = System.nanoTime();
            **TestSingleton.getInstance();**
            long end = System.nanoTime();
            return end - start;
        }
    };

    for (int i = 0; i < 100000; i++) {
        futList.add(es1.submit(task));
    }

    for (Future<Long> fut : futList) {
        try {
            totalTime1.getAndAdd(fut.get());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    System.out.println("Time taken by S1   " + totalTime1.get());
            .
            .
            ExecutorService es2 = Executors.newFixedThreadPool(threadpool);

    Callable<Long> task1 = new Callable<Long>() {
        @Override
        public Long call() throws Exception {
            long start = System.nanoTime();
            Test2.getInstance();
            long end = System.nanoTime();
            return end - start;
        }
    };

    for (int i = 0; i < 100000; i++) {
        futList1.add(es2.submit(task1));
    }

    for (Future<Long> fut : futList1) {
        try {
            totalTime2.getAndAdd(fut.get());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    System.out.println("Time taken by S2   " + totalTime2.get());

The results I got is:

Time taken by S1 4636498 Time taken by S2 5127865

First question is this the correct approach? and second even if I comment the getinstances method in both the call(), I get different times of execution of two identical blocks:

Time taken by S1 1506640 Time taken by S2 2156172

Jaap
  • 81,064
  • 34
  • 182
  • 193

1 Answers1

0

Don't measure each execution and sum the times, there will be too much inaccuracy in the individual measurements. Instead, get start time, execute 100000 times, get end time. Also, execute a few 1000 times before you start measuring to avoid skewing by start-up costs.

Mike Stockdale
  • 5,256
  • 3
  • 29
  • 33