Creating objects happens very quickly when the object is small and there is no GC cost.
final int batch = 1000 * 1000; Double[] doubles = new Double[batch]; long start = System.nanoTime(); for (int j = 0; j < batch; j++) doubles[j] = (double) j; long time = System.nanoTime() - start; System.out.printf("Average object allocation took %.1f ns.%n", (double) time/batch);
prints with -verbosegc
Average object allocation took 13.0 ns.
Note: no GC has occurred. However, increase the size, and the program needs to wait to copy the memory to the GC.
final int batch = 10 *1000 * 1000;
prints
[GC 96704K->94774K(370496K), 0.0862160 secs] [GC 191478K->187990K(467200K), 0.4135520 secs] [Full GC 187990K->187974K(618048K), 0.2339020 secs] Average object allocation took 78.6 ns.
I suspect your distribution is relatively slow because you are doing GC. One way is to increase the availability of memory for the application. (Although this may just delay the cost)
If I run it again using -verbosegc -XX:NewSize=1g
Average object allocation took 9.1 ns.
source share