Do comments slow down an interpreted language?

后端 未结 11 2222
深忆病人
深忆病人 2020-12-01 09:59

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

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 10:46

    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.

提交回复
热议问题