Do comments slow down an interpreted language?

后端 未结 11 2220
深忆病人
深忆病人 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:48

    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.

提交回复
热议问题