Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do comments slow down a compiled language?

Tags:

c

c#

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?

like image 234
jsls Avatar asked Jun 08 '16 09:06

jsls


People also ask

Do comments affect compile time?

Yes, each comment that you write will make the compilation slower, because the compiler has to read more text.

Do comments slow down a program?

So no, comments do not slow down the programs at run-time. Note: Interpreters that do not use some other inner structure to represent the code other than text, ie a syntax tree, must do exactly what you mentioned.

Does comments affect code 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.

Do comments slow down code JS?

Whitespaces and comments increase the size of the JavaScript file, which slows down the actual downloading of the file from the server - minification is the process of stripping unnecessary characters from a JavaScript file to make it smaller and easier to download.


2 Answers

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.

like image 136
Dmitry Bychenko Avatar answered Sep 16 '22 19:09

Dmitry Bychenko


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.

like image 25
Aleksandar Makragić Avatar answered Sep 17 '22 19:09

Aleksandar Makragić