Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 305
Gokul Nath KP Avatar asked Sep 16 '13 05:09

Gokul Nath KP


People also ask

Do comments increase execution time?

In an interpreted language, comments can indeed make a program's execution time slower, because the comments have to be parsed (and thrown away) every time you run the program.

Can too many comments slow down your code?

Commenting will not affect the script execution time in normal case. But the number of lines you write in your code affect the parser to read and buffer it considerably.

Do comments affect performance?

No it does not effect performance. As most of the programming languages compile or interpret code and comments are skipped as computer has nothing to do with comments.

What is effect on increasing the number of threads on overall execution time?

On a machine with multiple cores, the execution time should decrease with moderate amount of threads for long running tasks.


3 Answers

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.

like image 174
Jon Skeet Avatar answered Oct 09 '22 11:10

Jon Skeet


No, comments are ignored by compile so they cannot affect the time execution. The difference you get is very low. If you try your test 10 times you can see that the difference you get is in the range of statistical mistake.

Computer does a lot of tasks simultaneously. If you want to compare performance of 2 pieces of code that give similar execution time you need many experiments to prove that one of them is faster than other.

like image 2
AlexR Avatar answered Oct 09 '22 12:10

AlexR


It may slow the compiling process down on an extremely minute level - all it's doing is reading the // or /* */ and skipping everything after that until a line break or end comment, respectively. If you want to get more accurate results, run each iteration 10 times, and get the average execution time for each one. Then, compare.

like image 2
IHazABone Avatar answered Oct 09 '22 11:10

IHazABone