当我正在研究通过使用 System.currentTimeinMillis() 来测量 Java 中经过的执行时间时,如果代码如下所示,但我的查询是我们在编码时除了测量时间之外还应该记住的其他性能技术,我的查询更多地集中在性能优化技术上。
public class MeasureTimeExampleJava {
public static void main(String args[]) {
//measuring elapsed time using System.nanoTime
long startTime = System.nanoTime();
for(int i=0; i< 1000000; i++){
Object obj = new Object();
}
long elapsedTime = System.nanoTime() - startTime;
System.out.println("Total execution time to create 1000K objects in Java in millis: "
+ elapsedTime/1000000);
//measuring elapsed time using Spring StopWatch
StopWatch watch = new StopWatch();
watch.start();
for(int i=0; i< 1000000; i++){
Object obj = new Object();
}
watch.stop();
System.out.println("Total execution time to create 1000K objects in Java using StopWatch in millis: "
+ watch.getTotalTimeMillis());
}
}
Output:
Total execution time to create 1000K objects in Java in millis: 18
Total execution time to create 1000K objects in Java using StopWatch in millis: 15