I am asking this because I use Python, but it could apply to other interpreted languages as well (Ruby, PHP, JavaScript).
Am I slowing down the interpreter whenever
Having comments will slow down the startup time, as the scripts will get parsed into an executable form. However, in most cases comments don't slow down runtime.
Additionally in python, you can compile the .py files into .pyc, which won't contain the comments (I should hope) - this means that you won't get a startup hit either if the script is already compiled.
I wonder if it matters on how comments are used. For example, triple quotes is a docstring. If you use them, the content is validated. I ran into a problem awhile back where I was importing a library into my Python 3 code... I got this error regarding syntax on \N. I looked at the line number and it was content within a triple quote comment. I was somewhat surprised. New to Python, I never thought a block comment would be interpreted for syntax errors.
Simply if you type:
'''
(i.e. \Device\NPF_..)
'''
Python 2 doesn't throw an error, but Python 3 reports: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 14-15: malformed \N character escape
So Python 3 is evidently interpreting the triple quote, making sure it's valid syntax.
However, if turned into a single line comment: # (i.e. \Device\NPF_..)
No error results.
I wonder if the triple quote comments wer replaced with single lines, if a performance change would be seen.
My limited understanding of an interpreter is that it reads program expressions in as strings and converts those strings into code.
Most interpreters read the text (code) and produce an Abstract Syntax Tree data structure.
That structure contains no code, in text form, and of course no comments either. Just that tree is enough for executing programs. But interpreters, for efficiency reasons, go one step further and produce byte code. And Python does exactly that.
We could say that the code and the comments, in the form you wrote them, are simply not present,
when the program is running. So no, comments do not slow down the programs at run-time.
(*) 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. Interpret again and again the code at run-time.
As the other answers have already stated, a modern interpreted language like Python first parses and compiles the source into bytecode, and the parser simply ignores the comments. This clearly means that any loss of speed would only occur at startup when the source is actually parsed.
Because the parser ignores comments, the compiling phase is basically unaffected by any comments you put in. But the bytes in the comments themselves are actually being read in, and then skipped over during parsing. This means, if you have a crazy amount of comments (e.g. many hundreds of megabytes), this would slow down the interpreter. But then again this would slow any compiler as well.
Comments are usually stripped out in or before the parsing stage, and parsing is very fast, so effectively comments will not slow down the initialization time.