Do comments slow down an interpreted language? Do comments slow down an interpreted language? python python

Do comments slow down an interpreted language?


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.


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 BibleThe First Book of Moses:  Called Genesis1:1 In the beginning God created the heaven and the earth.1:2 And the earth was without form, and void; and darkness was uponthe face of the deep. And the Spirit of God moved upon the face of thewaters.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.


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.