測定するまでもない気がしますが、
現在日時を意味する java.util.Date のインスタンスを生成するのに以下のいずれが早いのかを確認してみます。
new java.util.Date()
java.util.Calendar.getInstance().getTime()
JMHでの測定用のコードは以下です
import java.util.Calendar; import java.util.Date; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Mode; import org.openjdk.jmh.runner.Runner; import org.openjdk.jmh.runner.RunnerException; import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; public class DateBenchmark { @Benchmark public void calendar() { Date d = Calendar.getInstance().getTime(); } @Benchmark public void date() { Date d = new Date(); } public static void main(String[] args) throws RunnerException { Options opt = new OptionsBuilder() .include(DateBenchmark.class.getSimpleName()) .warmupIterations(15) .forks(1) .mode(Mode.Throughput) .build(); new Runner(opt).run(); } }
結果:
# Run complete. Total time: 00:01:10 Benchmark Mode Cnt Score Error Units DateBenchmark.calendar thrpt 20 6855062.594 ± 63970.844 ops/s DateBenchmark.date thrpt 20 96526275.981 ± 214128.550 ops/s
ごらんの通り、結果が1桁違っています。Calendar経由するとかなり遅くなっています