Do comments slow down an interpreted language?

The effect is negligable for everyday usage. It's easy to test, but if you consider a simple loop such as:

For N = 1 To 100000: Next

Your computer can process that (count to 100,000) quicker than you can blink. Ignoring a line of text that starts with a certain character will be more than 10,000 times faster.

Don't worry about it.


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.


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.


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.