9

This question has been asked several times here about interpreted language or R but I don't find anything about a compiled language, specifically C#.

According to what I read it seems to be negligible but it still induces a very little slowdown on interpreted language: Do comments slow down an interpreted language?

What about compiled languages?! do they slow down only the compilation process but not execution?

fluffy
  • 5,212
  • 2
  • 37
  • 67
jsls
  • 251
  • 3
  • 16
  • 7
    Comments are stripped from the compiled code, they can not slow down your compiled program. – Gar Jun 08 '16 at 09:50
  • 1
    afaik: nope. comments get scraped by the compiler. also, see: http://stackoverflow.com/questions/821510/comments-compiled-into-exe-in-net – garglblarg Jun 08 '16 at 09:51
  • 2
    All the comments can do is to slow down the compiler (it should parse the source code and cut the comments off). – Dmitry Bychenko Jun 08 '16 at 09:52
  • 2
    To even consider this implies considering coding with minimal or no comments - therefore it's not worth considering! – noelicus Jun 08 '16 at 09:53
  • 4
    Sounds like someone searching for arguments to answer the question "why don't you comment your code" :) – Evk Jun 08 '16 at 09:54
  • 3
    @Gar Detail: not _stripped_ so much as replaced: "Each comment is replaced by one space character." – chux - Reinstate Monica Jun 08 '16 at 16:20

5 Answers5

20

When you compile the program, the compiler actually does:

  • Lexical Analysis (tokenization)
  • Syntax Analysis (parsing)
  • Semantic Analysis (language rules checked)
  • [Intermediate] Code Generation
  • Code Optimization (optional)

As for comments they should be extracted as tokens on Lexical Analysis stage and dropped out on Syntax Analysis (parsing) stage. So you can slow down the compiler, but not the code generated.

Many interpreted languages often do first two or three stages and only then execute, so comments don't necessarily slow down even interpreted languages.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 1
    It’s worth noting that the same is true for virtually all modern *interpreted* languages. So even interpreted languages won’t see a slow-down from comments (except in the first stage of parsing, which, regardless of code flow, happens only once). – Konrad Rudolph Jun 08 '16 at 10:04
  • I wouldn't say code optimization is optional. Every modern compiler has code optimization. It is very important phase of compilation. – Aleksandar Makragić Jun 08 '16 at 10:07
  • It is one of the main reasons why it is questionable whether you can write faster Assembler code for some critical section then compiler would optimize your regular code. – Aleksandar Makragić Jun 08 '16 at 10:08
  • 1
    @Aleksandar Makragić: often you can switch on kind of *debug mode*: ask the the compiler to perform *exactly* what you've put down, without trying to optimize it anyhow. – Dmitry Bychenko Jun 08 '16 at 10:10
  • @Konrad Rudolph: Thank you! Your note is really worth mentioned. – Dmitry Bychenko Jun 08 '16 at 10:14
  • Can you explain that little better? I'm not arguing, I just wanna learn something new :) I mean I guess it could be optional, but you would get about 20% slower performance. I think GCC has about 20% faster code because of code optimization. LLVM is I think somewhere around 15%. These numbers I wrote may be few percent wrong. – Aleksandar Makragić Jun 08 '16 at 10:15
  • 2
    @Aleksandar Makragić: Imaging that you have a hidden error in your code and so you want to debug it. If code is optimized, it can well be a difficult task: many variables/methods/functions will be reordered, dropped, rewritten in some other (often very pecular) way. So you say: I'm ready to have, say, 30% slower code, but, please, do *exactly* what I put; and only when I find out the error, optimize my *final* solution. First right, then fast: do not optimize prematurely! – Dmitry Bychenko Jun 08 '16 at 10:24
  • @DmitryBychenko Danke. – Aleksandar Makragić Jun 08 '16 at 10:26
4

Compilers have these phases:

  • Lexical analysis
  • Syntax analysis
  • Semantic analysis
  • Generating machine independent code
  • Code optimization
  • Generating final code

In Lexical analysis, comments are skipped, it is basically like you didn't write anything.

So no, they do not affect performance in any way.

Aleksandar Makragić
  • 1,957
  • 17
  • 32
3

Comment will be exclude from Lexical analysis phase

but if code with comments are parsed in Lexical analysis phase it might be take more time compare as code without comments but comments does not affect execution time

Why are you not try by your self, look into below example and run it.

class Program
{
    static void Main(string[] args)
    {
        DateTime StartTime;
        DateTime EndTime;
        TimeSpan ExecutionTime;

        StartTime = DateTime.Now;
        for (int i = 0; i < 10000;)
        {
            i++;
            for (int j = 0; j < 100000;)
            {
                j++;
            }
        }
        EndTime = DateTime.Now;
        ExecutionTime = (EndTime - StartTime);

        Console.WriteLine("Phase 1 without comment done.");
        Console.WriteLine("Phase 1 start time : " + StartTime.ToString());
        Console.WriteLine("Phase 1 end time : " + EndTime.ToString());
        Console.WriteLine("Phase 1 Execution Seconds : " + ExecutionTime.TotalSeconds);

        Console.WriteLine("\r\n---------------------------------------------\r\n");

        StartTime = DateTime.Now;
        for (int i = 0; i < 10000;)
        {
            i++;
            for (int j = 0; j < 100000;)
            {
                j++;
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
                // SOME COMMENT.
            }
        }
        EndTime = DateTime.Now;
        ExecutionTime = (EndTime - StartTime);

        Console.WriteLine("Phase 2 with comment done.");
        Console.WriteLine("Phase 2 start time : " + StartTime.ToString());
        Console.WriteLine("Phase 2 end time : " + EndTime.ToString());
        Console.WriteLine("Phase 2 Execution Seconds : " + ExecutionTime.TotalSeconds);

        Console.ReadKey();
    }
}
Darshan Faldu
  • 1,471
  • 2
  • 15
  • 32
0

No the compiler does not slow down the program, it ignores all the comments and takes the raw code.

Yousuf Azad
  • 414
  • 1
  • 7
  • 17
-1

If language is using the compiler then the processing don't effected but if interpreter is working there then the process may slow down due to comments.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 22 '21 at 06:27