programing

댓글이 통역 언어를 느리게합니까?

nasanasas 2020. 11. 14. 10:17
반응형

댓글이 통역 언어를 느리게합니까?


파이썬을 사용하기 때문에 이것을 묻고 있지만 다른 해석 언어 (Ruby, PHP, JavaScript)에도 적용될 수 있습니다.

코드에 주석을 남길 때마다 인터프리터 속도가 느려지나요? 인터프리터에 대한 제한된 이해에 따르면 프로그램 표현식을 문자열로 읽은 다음 해당 문자열을 코드로 변환합니다. 댓글을 구문 분석 할 때마다 시간이 낭비되는 것 같습니다.

이것이 사실입니까? 통역 언어로 된 주석에 대한 규칙이 있습니까? 아니면 그 효과가 무시할 만합니까?


Python의 경우 소스 파일 ( .pyc파일) 이 실행되기 전에 컴파일되고 프로세스에서 주석이 제거됩니다. 따라서 주석 너무 많으면 컴파일 시간 느려질 있지만 실행 시간에는 영향을주지 않습니다.


글쎄, 나는 다음과 같은 짧은 파이썬 프로그램을 썼다.

for i in range (1,1000000):
    a = i*10

아이디어는 간단한 계산을 여러 번 수행하는 것입니다.

그 시점에서 실행하는데 0.35 ± 0.01 초가 걸렸습니다.

그런 다음 킹 제임스 성경 전체를 다음과 같이 삽입하여 다시 썼습니다.

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

이번에는 실행하는 데 0.4 ± 0.05 초가 걸렸습니다.

그래서 대답은 ' 예' 입니다. 루프에서 4MB의 주석은 상당한 차이를 만듭니다.


주석은 일반적으로 구문 분석 단계 또는 그 이전에 제거되며 구문 분석이 매우 빠르므로 효과적으로 주석이 초기화 시간을 늦추지 않습니다.


Rich 's와 같은 스크립트를 몇 가지 주석으로 작성했습니다 (약 500kb 텍스트).

# -*- coding: iso-8859-15 -*-
import timeit

no_comments = """
a = 30
b = 40
for i in range(10):
    c = a**i * b**i
"""
yes_comment = """
a = 30
b = 40

# full HTML from http://en.wikipedia.org/
# wiki/Line_of_succession_to_the_British_throne

for i in range(10):
    c = a**i * b**i
"""
loopcomment = """
a = 30
b = 40

for i in range(10):
    # full HTML from http://en.wikipedia.org/
    # wiki/Line_of_succession_to_the_British_throne

    c = a**i * b**i
"""

t_n = timeit.Timer(stmt=no_comments)
t_y = timeit.Timer(stmt=yes_comment)
t_l = timeit.Timer(stmt=loopcomment)

print "Uncommented block takes %.2f usec/pass" % (
    1e6 * t_n.timeit(number=100000)/1e5)
print "Commented block takes %.2f usec/pass" % (
    1e6 * t_y.timeit(number=100000)/1e5)
print "Commented block (in loop) takes %.2f usec/pass" % (
    1e6 * t_l.timeit(number=100000)/1e5)


C:\Scripts>timecomment.py
Uncommented block takes 15.44 usec/pass
Commented block takes 15.38 usec/pass
Commented block (in loop) takes 15.57 usec/pass

C:\Scripts>timecomment.py
Uncommented block takes 15.10 usec/pass
Commented block takes 14.99 usec/pass
Commented block (in loop) takes 14.95 usec/pass

C:\Scripts>timecomment.py
Uncommented block takes 15.52 usec/pass
Commented block takes 15.42 usec/pass
Commented block (in loop) takes 15.45 usec/pass

David의 의견에 따라 편집 :

 -*- coding: iso-8859-15 -*-
import timeit

init = "a = 30\nb = 40\n"
for_ = "for i in range(10):"
loop = "%sc = a**%s * b**%s"
historylesson = """
# <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
# blah blah...
# --></body></html> 
"""
tabhistorylesson = """
    # <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    # blah blah...
    # --></body></html> 
"""

s_looped = init + "\n" + for_ + "\n" + tabhistorylesson + loop % ('   ','i','i')
s_unroll = init + "\n"
for i in range(10):
    s_unroll += historylesson + "\n" + loop % ('',i,i) + "\n"
t_looped = timeit.Timer(stmt=s_looped)
t_unroll = timeit.Timer(stmt=s_unroll)

print "Looped length: %i, unrolled: %i." % (len(s_looped), len(s_unroll))

print "For block takes %.2f usec/pass" % (
    1e6 * t_looped.timeit(number=100000)/1e5)
print "Unrolled it takes %.2f usec/pass" % (
    1e6 * t_unroll.timeit(number=100000)/1e5)


C:\Scripts>timecomment_unroll.py
Looped length: 623604, unrolled: 5881926.
For block takes 15.12 usec/pass
Unrolled it takes 14.21 usec/pass

C:\Scripts>timecomment_unroll.py
Looped length: 623604, unrolled: 5881926.
For block takes 15.43 usec/pass
Unrolled it takes 14.63 usec/pass

C:\Scripts>timecomment_unroll.py
Looped length: 623604, unrolled: 5881926.
For block takes 15.10 usec/pass
Unrolled it takes 14.22 usec/pass

이 효과는 일상적인 사용에 대해 무시할 수 있습니다. 테스트하기는 쉽지만 다음과 같은 간단한 루프를 고려한다면 :

For N = 1 To 100000: Next

컴퓨터는 눈을 깜빡이는 것보다 빠르게 처리 할 수 ​​있습니다 (100,000까지 계산). 특정 문자로 시작하는 텍스트 줄을 무시하면 10,000 배 이상 빠릅니다.

그것에 대해 걱정하지 마십시오.


인터프리터가 어떻게 구현되었는지에 따라 다릅니다. 가장 합리적으로 현대적인 인터프리터는 실제 실행 전에 소스 코드에 대해 최소한 약간의 사전 처리를 수행하며 여기에는 주석을 제거하여 그 시점 이후에는 아무런 차이가 없습니다.

At one time, when memory was severely constrained (e.g., 64K total addressable memory, and cassette tapes for storage) you couldn't take things like that for granted. Back in the day of the Apple II, Commodore PET, TRS-80, etc., it was fairly routine for programmers to explicitly remove comments (and even white-space) to improve execution speed. This was also only one of many source code-level hacks routinely employed at the time1.

Of course, it also helped that those machines had CPUs that could only execute one instruction at a time, had clock speeds around 1 MHz, and had only 8-bit processor registers. Even a machine you'd now find only in a dumpster is so much faster than those were that it's not even funny...


1. For another example, in Applesoft you could gain or lose a little speed depending on how you numbered lines. If memory serves, the speed gain was when the target of a goto statement was a multiple of 16.


Having comments will slow down the startup time, as the scripts will get parsed into an executable form. However, in most cases comments don't slow down runtime.

Additionally in python, you can compile the .py files into .pyc, which won't contain the comments (I should hope) - this means that you won't get a startup hit either if the script is already compiled.


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.


As the other answers have already stated, a modern interpreted language like Python first parses and compiles the source into bytecode, and the parser simply ignores the comments. This clearly means that any loss of speed would only occur at startup when the source is actually parsed.

Because the parser ignores comments, the compiling phase is basically unaffected by any comments you put in. But the bytes in the comments themselves are actually being read in, and then skipped over during parsing. This means, if you have a crazy amount of comments (e.g. many hundreds of megabytes), this would slow down the interpreter. But then again this would slow any compiler as well.


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.


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.

참고URL : https://stackoverflow.com/questions/2731022/do-comments-slow-down-an-interpreted-language

반응형