TheDeveloperBlog.com


C# Comment Program

Comments do not affect a program's execution speed. Some programming languages have been affected in some way by comments in the program text. But C# is a compiled language where comments do not affect the executable.

Compiler Phases

Example. We see a program that defines two methods that do the same thing. The two methods, Increment and IncrementWithComments, are identical except that the first has no comments and the second has almost 20 lines of comments on it.

So: By testing these two methods, we can show what effect comments in methods has on execution performance for the C# language.

C# program that has comments

class Program
{
    static void Increment()
    {
	int value = 0;
	for (int i = 0; i < 100000; i++)
	{
	    for (int j = 0; j < 10; j++)
	    {
		value++;
	    }
	}
	_field = value;
    }

    static void IncrementWithComments()
    {
	// Local variable in variable slot that is incremented.
	int value = 0;
	// Loop through the first one-hundred thousand numbers.
	for (int i = 0; i < 100000; i++)
	{
	    // Nested loop through first ten numbers.
	    for (int j = 0; j < 10; j++)
	    {
		// Increment the variable once.
		value++;
	    }
	}
	// Store local variable's value in static field.
	_field = value;
    }

    static int _field;

    static void Main()
    {
	Increment();
	IncrementWithComments();
    }
}

Benchmark results

Increment:             0.331 ms (uncommented release)
IncrementWithComments: 0.330 ms (commented release)

Increment:             0.308 ms (uncommented debug, fewer iterations)
IncrementWithComments: 0.308 ms (commented release, fewer iterations)

The program contains three methods: two methods that increment a local variable and the Main entry point that calls those methods. The key point here is that the first method, Increment, contains no comments.

And: The second method, IncrementWithComments, has the same logic but has almost 20 lines of comments in it.

IncrementWithComments shows two different kinds of comments. First, it shows XML-style comments at the level of the method signature. XML comments have three "/" characters at their start. They can be used to generate documentation.

Note: XML comments are not useful for local variables inside methods. They will compile but have no special use.

XML

Benchmark. The methods were tested against each other in loops. To improve the quality of the results, the methods were called once before the timed loops to ensure they were JIT compiled. The benchmark was executed in DEBUG and RELEASE.

Benchmark

Result: The times measured were close for both the uncommented and commented versions of the methods.


Compiler phases. Compilers are logically organized into phases, which are grouped into passes. Early in the compilation cycle, a lexical parser that converts the program text into a series of lexemes, which is compiler-terminology for program "words".

Token

At this point, the compiler can remove all lines beginning with the // characters and text between the /* and */ delimiters. Because of this phase, the compiled DLL from the C# code contains no trace of the comments.


Intent. It is usually recommended to describe at the level of intent, not the level of implementation. So you can comment on what the code is supposed to do, not how it actually does that. You can focus on "why" and "what" and not on "how".

Tip: The book Code Complete by Steve McConnell contains a large chapter on commenting strategies.


IL Disassembler. It is possible to see how the C# compiler translates programs into the intermediate language of the .NET Framework. IL Disassembler that reads in the compiled program and represent the abstract binary format.

IL DisassemblerIntermediate Language

Note: This code will have no comments or local variable names because these were removed.


Summary. We saw how comments do not affect the resulting executable sources or the intermediate language. Comments have no effect on the performance of C# programs. There are relevant compiler concepts and style issues.