Do comments slow down an interpreted language?

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

    This question is really old, but after reading the accepted answer which claims that it won't impact the execution time, which is wrong, I am giving you a simple example where you can see and check the amount it influences the execution time indeed.
    I have a file called constants.py. It contains all different actions of chess in a list:

    LABELS = [ "a1b1"
        "a1c1", 
        "a1d1", 
        "a1e1", 
        "a1f1",....]
    

    The list LABELS contains 2272 elements. In another file I call:

    import constants
    np.array(constants.LABELS)
    

    I measured it ten times and the execution of the code takes about 0.597 ms. Now I changed the file and inserted next to each element (2272 times) a comment:

    LABELS = [ "a1b1",  # 0 
                "a1c1", # 1
                "a1d1", # 2
                "a1e1", # 3
                "a1f1", # 4
                 ...,
                "Q@h8", # 2271]
    

    Now after measuring the execution time of np.array(constants.LABELS) ten times, I have an average execution time of 4.28 ms, thus, about 7 times slower.
    Therefore, yes, it impacts the execution time if you have lots of comments.

提交回复
热议问题