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?
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.
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.
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.
On a machine with multiple cores, the execution time should decrease with moderate amount of threads for long running tasks.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With