考虑以下情况:
案例1:(少评论for loop
)
import java.io.IOException;
public class Stopwatch {
private static long start;
public static void main(String args[]) throws IOException {
start = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++) {
/**
* Comment Line 1
* Comment Line 2
* Comment Line 3
* Comment Line 4
*/
}
System.out.println("The time taken to execute the code is: " + (System.currentTimeMillis() - start)/1000.0);
}
}
执行代码所用时间为:2.259
案例2:(更多评论在for loop
)
import java.io.IOException;
public class Stopwatch {
private static long start;
public static void main(String args[]) throws IOException {
start = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++) {
/**
* Comment Line 1
* Comment Line 2
* Comment Line 3
* Comment Line 4
* Comment Line 5
* Comment Line 6
* Comment Line 7
* Comment Line 8
*/
}
System.out.println("The time taken to execute the code is: " + (System.currentTimeMillis() - start)/1000.0);
}
}
执行代码所用时间为:2.279
案例3:(无评论,空for loop
)
import java.io.IOException;
public class Stopwatch {
private static long start;
public static void main(String args[]) throws IOException {
start = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++) {
}
System.out.println("The time taken to execute the code is: " + (System.currentTimeMillis() - start)/1000.0);
}
}
执行代码所用时间为:2.249
配置:JDK 1.5、第三代 i5、4GB 内存。
问题:如果我们添加更多注释,程序是否需要更多时间来执行?为什么?