【发布时间】:2013-09-16 05:53:27
【问题描述】:
考虑以下情况:
案例 1:(for loop 中的 cmets 较少)
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:(更多cmets在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:(无cmets,空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 内存。
问题:如果我们添加更多的cmets,程序是否需要更多的时间来执行?为什么?
【问题讨论】:
标签: java comments execution-time jdk1.5