Do comments slow down an interpreted language?

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 I leave a comment in my code? According to my limited understanding of an interpreter, it reads program expressions in as strings and then converts those strings into code. It seems that every time it parses a comment, that is wasted time.

Is this the case? Is there some convention for comments in interpreted languages, or is the effect negligible?


Solution 1:

For the case of Python, source files are compiled before being executed (the .pyc files), and the comments are stripped in the process. So comments could slow down the compilation time if you have gazillions of them, but they won't impact the execution time.

Solution 2:

Well, I wrote a short python program like this:

for i in range (1,1000000):
    a = i*10

The idea is, do a simple calculation loads of times.

By timing that, it took 0.35±0.01 seconds to run.

I then rewrote it with the whole of the King James Bible inserted like this:

for i in range (1,1000000):
    """
The Old Testament of the King James Version of the Bible

The First Book of Moses:  Called Genesis


1:1 In the beginning God created the heaven and the earth.

1:2 And the earth was without form, and void; and darkness was upon
the face of the deep. And the Spirit of God moved upon the face of the
waters.

1:3 And God said, Let there be light: and there was light.

...
...
...
...

Even so, come, Lord Jesus.

22:21 The grace of our Lord Jesus Christ be with you all. Amen.
    """
    a = i*10

This time it took 0.4±0.05 seconds to run.

So the answer is yes. 4MB of comments in a loop make a measurable difference.