1

I was timing the following loop:

public static void main(String[] args) {
    long time = System.currentTimeMillis();
    List<Hashtable> objects = new ArrayList<Hashtable>(10000000);
    for (int i = 10000000; i > 0; i--) {
        objects.add(new Hashtable());
    }
    System.out.println(System.currentTimeMillis() - time);

}

In JDK 1.6.0_33 it takes around 2500 ms every time. Switching to JDK 1.7.0_25 it takes 5600 ms.

The same with just doing new Hashtable(), without adding it to a list: Java 1.6: 60 ms, Java 1.7: 460 ms.

This looks strange, did something deteriorate in Java7, did the internals of Hashtable become more complex, or am I just missing something?

Per
  • 636
  • 5
  • 8
  • 3
    Please, read first how to do microbenchmarks in Java. There can be lot's of things other than code performance that could cause such difference, e.g. different default JIT options that makes it slower. – Daniil Sep 29 '13 at 07:52
  • 1
    please have a look at this thread, it may help http://stackoverflow.com/questions/14010906/given-that-hashmaps-in-jdk1-6-and-above-cause-problems-with-multi-threading-how – S4beR Sep 29 '13 at 08:00
  • Running the above code in a loop ~~50 times, the jvm on my machine(java7) it averages around 2500ms. First few iterations are around 5000-6000ms, then it drops down to ~~2500. It is pretty clear when the JIT kicks in. – Zavior Sep 29 '13 at 08:14
  • @S4beR:Thanks, that seems to be it, no mystery any more! – Per Sep 29 '13 at 08:17

0 Answers0