【问题标题】:Does increase in the number of comments increases the execution time?评论数量的增加是否会增加执行时间?
【发布时间】: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


    【解决方案1】:

    问题:如果我们添加更多的cmets,程序是否需要更多的时间来执行?为什么?

    没有。注释对执行没有影响。

    它们会使编译器的速度变慢一点微小——但即使这样也应该是难以察觉的,除非你有一个可笑数量的cmets。

    您注意到的“效果”更多地与您计时的方式有关 - 使用 System.currentTimeMillis 进行基准测试是一个坏主意;您应该改用System.nanos,因为它通常使用更高精度的时钟(仅适用于计时,不适用于确定“挂钟”时间)。此外,通常基准程序应该在实际测量之前运行它们的“目标”代码足够长的时间来预热 JIT 编译器等。然后你需要考虑其他可能同时在你的系统上运行的东西。基本上,编写一个好的基准测试涉及很多内容。如果您将来要编写任何重要的基准测试,我建议您查看Caliper

    您可以验证仅仅由于 cmets 没有区别 - 编译您的代码然后运行 ​​

    javap -c Stopwatch
    

    您可以查看字节码。您会发现不同版本之间没有区别。

    【讨论】:

      【解决方案2】:

      不,编译时会忽略 cmets,因此它们不会影响时间执行。你得到的差异非常小。如果您尝试测试 10 次,您可以看到您得到的差异在统计错误范围内。

      计算机同时执行许多任务。如果你想比较两段执行时间相似的代码的性能,你需要做很多实验来证明其中一个比另一个更快。

      【讨论】:

        【解决方案3】:

        它可能会在极小的程度上减慢编译过程 - 它所做的只是读取 ///* */ 并跳过之后的所有内容,直到换行或结束注释。如果想得到更准确的结果,每次迭代运行 10 次,得到每次迭代的平均执行时间。然后,比较。

        【讨论】:

          【解决方案4】:

          添加 cmets 会增加编译程序所需的时间,但只会增加一分钟。编译时,编译器必须读取一行以了解它是否需要跳过(在注释的情况下)或执行该行代码。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-01-08
            • 1970-01-01
            • 2018-02-19
            • 2018-01-02
            • 2021-06-03
            • 1970-01-01
            • 2021-01-25
            • 2015-10-05
            相关资源
            最近更新 更多