Does the number of comments increase the execution time?

Consider the following cases:

Case 1: (fewer 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); } } 

Code Lead Time: 2,259

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

Code Lead Time: 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); } } 

Code Lead Time: 2.249

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

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

+6
source share
3 answers

Question. If we add more comments, will the program have more time to complete? Why?

No. Comments do not affect execution.

They will slow down the compiler with a small bit - but even this should be inconspicuous if you do not have a ridiculous amount of comments.

The β€œeffect” you notice is more about how you choose things β€” using System.currentTimeMillis for benchmarking is a bad idea; you should use System.nanos instead, since it usually uses a clock with higher accuracy (suitable only for synchronization, not for determining the time of a "wall clock"). In addition, usually test programs should run their β€œtarget” code long enough to warm up the JIT compiler, etc. Before the actual measurement. Then you need to consider other things that could be running on your system at the same time. Basically, there was a lot to do with writing a good test. I suggest you take a look at Caliper if you are planning to write any important landmarks in the future.

You can check for differences just because of the comments, though - compile your code and then run

 javap -c Stopwatch 

and you can look at the bytecode. You will not see the differences between the different versions.

+15
source

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

A computer performs many tasks simultaneously. If you want to compare the performance of two pieces of code that give the same runtime, you need a lot of experimentation to prove that one of them is faster than the other.

+1
source

This can slow down the compilation process to an extremely minimal level - all it does is read // or /* */ and skip everything after that until a line break or comment end, respectively. If you want to get more accurate results, run each iteration 10 times and get the average execution time for each of them. Then compare.

+1
source

Source: https://habr.com/ru/post/953892/


All Articles