javacommentsexecution-timejava-5

Does increase in the number of comments increases the execution time?


Consider the following cases:

Case 1: (Less comments in 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);
    }
}

The time taken to execute the code is: 2.259

Case 2: (More comments in 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);
    }
}

The time taken to execute the code is: 2.279

Case 3: (No comments, empty 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);
    }
}

The time taken to execute the code is: 2.249

Configuration: JDK 1.5, 3rd Gen i5, 4GB Ram.

Question: If we add more comments, does the program takes more time to execute? Why?


Solution

  • Question: If we add more comments, does the program takes more time to execute? Why?

    No. Comments have no effect on execution.

    They will slow the compiler down a tiny bit - but even that should be imperceptible unless you have a ridiculous number of comments.

    The "effect" you're noticing is more to do with the way you're timing things - using System.currentTimeMillis for benchmarking is a bad idea; you should use System.nanos instead as it typically uses a higher-accuracy clock (suitable only for timing, not for determining the "wall clock" time). Additionally, typically benchmark programs should run their "target" code for long enough to warm up the JIT compiler etc before actually measuring. Then you need to consider other things which might have been running on your system at the same time. Basically there's a lot involved in writing a good benchmark. I suggest you look at Caliper if you're going to be writing any significant benchmarks in the future.

    You can verify that there's no difference just due to the comments though - compile your code and then run

    javap -c Stopwatch
    

    and you can look at the bytecode. You'll see there's no difference between the different versions.