I am asking this because I use Python, but it could apply to other interpreted languages as well (Ruby, PHP, JavaScript).
我问这个是因为我使用Python,但它也适用于其他解释语言(Ruby,PHP,JavaScript)。
Am I slowing down the interpreter whenever I leave a comment in my code? According to my limited understanding of an interpreter, it reads program expressions in as strings and then converts those strings into code. It seems that every time it parses a comment, that is wasted time.
每当我在代码中留下评论时,我是否会放慢口译员的速度?根据我对解释器的有限理解,它以字符串形式读取程序表达式,然后将这些字符串转换为代码。似乎每次解析评论,都是浪费时间。
Is this the case? Is there some convention for comments in interpreted languages, or is the effect negligible?
是这样的吗?在解释语言中是否有一些评论惯例,或者效果可以忽略不计?
10 个解决方案
#1
67
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.
对于Python的情况,源文件在执行之前被编译(.pyc文件),并且在该过程中将删除注释。因此,如果你有很多文章,评论可能会减慢编译时间,但它们不会影响执行时间。
#2
19
Well, I wrote a short python program like this:
好吧,我写了一个这样的短python程序:
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.
通过计时,运行需要0.35±0.01秒。
I then rewrote it with the whole of the King James Bible inserted like this:
然后我用这样插入的整个King James Bible重写了它:
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
This time it took 0.4±0.05 seconds to run.
这次运行需要0.4±0.05秒。
So the answer is yes. 4MB of comments in a loop make a measurable difference.
所以答案是肯定的。循环中的4MB注释会产生可测量的差异。
#3
18
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.
注释通常在解析阶段或之前被删除,并且解析非常快,因此有效注释不会减慢初始化时间。
#4
5
Did up a script like Rich's with some comments (only about 500kb text):
像Rich这样的脚本做了一些评论(只有大约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
Edit as per David's comment:
根据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
#5
4
The effect is negligable for everyday usage. It's easy to test, but if you consider a simple loop such as:
这种效果对于日常使用来说是可以忽略的。它很容易测试,但如果你考虑一个简单的循环,例如:
For N = 1 To 100000: Next
Your computer can process that (count to 100,000) quicker than you can blink. Ignoring a line of text that starts with a certain character will be more than 10,000 times faster.
您的计算机可以比闪烁更快地处理(计数到100,000)。忽略以某个字符开头的文本行将快10,000倍以上。
Don't worry about it.
别担心。
#6
4
It depends on how the interpreter is implemented. Most reasonably modern interpreters do at least a bit of pre-processing on the source code before any actual execution, and that will include stripping out the comments so they make no difference from that point onward.
这取决于解释器的实现方式。最合理的现代解释器在任何实际执行之前至少对源代码进行一些预处理,这将包括删除注释,以便它们从那一点开始没有区别。
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.
有一段时间,当内存受到严重限制时(例如,64K总可寻址内存,以及用于存储的盒式磁带),你不能认为这样的事情是理所当然的。早在Apple II,Commodore PET,TRS-80等当天,程序员明确删除注释(甚至是空白空间)以提高执行速度是相当常规的。这也只是当时常规使用的许多源代码级别的黑客之一1。
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...
当然,这也有助于这些机器的CPU只能一次执行一条指令,时钟速度约为1 MHz,并且只有8位处理器寄存器。即使是现在只能在垃圾箱中找到的机器也比那些甚至不好笑的机器快得多......
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.
1.另一个例子,在Applesoft中,你可以获得或失去一点速度,具体取决于你的编号方式。如果内存服务,速度增益是当goto语句的目标是16的倍数时。
#7
1
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.
另外在python中,你可以将.py文件编译成.pyc,它不包含注释(我希望) - 这意味着如果脚本已经编译,你也不会得到启动命中。
#8
1
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.
大多数解释器读取文本(代码)并生成抽象语法树数据结构。该结构不包含文本形式的代码,当然也没有注释。只有那棵树足以执行程序。但是出于效率原因,解释器更进一步并产生字节代码。而Python就是这样做的。
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.
(*)不使用其他内部结构来表示除文本之外的代码(即语法树)的解释器必须完全按照您的提及进行操作。在运行时一次又一次地解释代码。
#9
0
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.
正如其他答案已经陈述的那样,像Python这样的现代解释语言首先将源解析并编译为字节码,而解析器只是忽略了注释。这显然意味着任何速度损失只会在启动时实际解析源。
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.
因为解析器忽略了注释,所以编译阶段基本上不受您放入的任何注释的影响。但是注释本身中的字节实际上是被读入的,然后在解析期间被跳过。这意味着,如果您有大量的评论(例如数百兆字节),这将减慢解释器的速度。但是,这也会减慢任何编译器的速度。
#10
0
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.
我想知道如何使用评论。例如,三重引号是文档字符串。如果您使用它们,则会验证内容。我在一段时间内遇到了一个问题,我将一个库导入我的Python 3代码......我在\ N上遇到了关于语法的错误。我查看了行号,它是三重引用评论中的内容。我有点惊讶。 Python新手,我从未想过会因语法错误而解释块注释。
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
Python 2不会抛出错误,但Python 3报告:SyntaxError :( unicode error)'unicodeescape'编解码器无法解码位置14-15中的字节:格式错误\ N字符转义
So Python 3 is evidently interpreting the triple quote, making sure it's valid syntax.
所以Python 3显然是在解释三重引号,确保它是有效的语法。
However, if turned into a single line comment: # (i.e. \Device\NPF_..)
No error results.
但是,如果转换为单行注释:#(即\ Device \ NPF_ ..)没有错误结果。
I wonder if the triple quote comments wer replaced with single lines, if a performance change would be seen.
如果可以看到性能变化,我想知道三重引用评论是否被单行代替。
#1
67
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.
对于Python的情况,源文件在执行之前被编译(.pyc文件),并且在该过程中将删除注释。因此,如果你有很多文章,评论可能会减慢编译时间,但它们不会影响执行时间。
#2
19
Well, I wrote a short python program like this:
好吧,我写了一个这样的短python程序:
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.
通过计时,运行需要0.35±0.01秒。
I then rewrote it with the whole of the King James Bible inserted like this:
然后我用这样插入的整个King James Bible重写了它:
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
This time it took 0.4±0.05 seconds to run.
这次运行需要0.4±0.05秒。
So the answer is yes. 4MB of comments in a loop make a measurable difference.
所以答案是肯定的。循环中的4MB注释会产生可测量的差异。
#3
18
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.
注释通常在解析阶段或之前被删除,并且解析非常快,因此有效注释不会减慢初始化时间。
#4
5
Did up a script like Rich's with some comments (only about 500kb text):
像Rich这样的脚本做了一些评论(只有大约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
Edit as per David's comment:
根据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
#5
4
The effect is negligable for everyday usage. It's easy to test, but if you consider a simple loop such as:
这种效果对于日常使用来说是可以忽略的。它很容易测试,但如果你考虑一个简单的循环,例如:
For N = 1 To 100000: Next
Your computer can process that (count to 100,000) quicker than you can blink. Ignoring a line of text that starts with a certain character will be more than 10,000 times faster.
您的计算机可以比闪烁更快地处理(计数到100,000)。忽略以某个字符开头的文本行将快10,000倍以上。
Don't worry about it.
别担心。
#6
4
It depends on how the interpreter is implemented. Most reasonably modern interpreters do at least a bit of pre-processing on the source code before any actual execution, and that will include stripping out the comments so they make no difference from that point onward.
这取决于解释器的实现方式。最合理的现代解释器在任何实际执行之前至少对源代码进行一些预处理,这将包括删除注释,以便它们从那一点开始没有区别。
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.
有一段时间,当内存受到严重限制时(例如,64K总可寻址内存,以及用于存储的盒式磁带),你不能认为这样的事情是理所当然的。早在Apple II,Commodore PET,TRS-80等当天,程序员明确删除注释(甚至是空白空间)以提高执行速度是相当常规的。这也只是当时常规使用的许多源代码级别的黑客之一1。
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...
当然,这也有助于这些机器的CPU只能一次执行一条指令,时钟速度约为1 MHz,并且只有8位处理器寄存器。即使是现在只能在垃圾箱中找到的机器也比那些甚至不好笑的机器快得多......
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.
1.另一个例子,在Applesoft中,你可以获得或失去一点速度,具体取决于你的编号方式。如果内存服务,速度增益是当goto语句的目标是16的倍数时。
#7
1
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.
另外在python中,你可以将.py文件编译成.pyc,它不包含注释(我希望) - 这意味着如果脚本已经编译,你也不会得到启动命中。
#8
1
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.
大多数解释器读取文本(代码)并生成抽象语法树数据结构。该结构不包含文本形式的代码,当然也没有注释。只有那棵树足以执行程序。但是出于效率原因,解释器更进一步并产生字节代码。而Python就是这样做的。
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.
(*)不使用其他内部结构来表示除文本之外的代码(即语法树)的解释器必须完全按照您的提及进行操作。在运行时一次又一次地解释代码。
#9
0
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.
正如其他答案已经陈述的那样,像Python这样的现代解释语言首先将源解析并编译为字节码,而解析器只是忽略了注释。这显然意味着任何速度损失只会在启动时实际解析源。
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.
因为解析器忽略了注释,所以编译阶段基本上不受您放入的任何注释的影响。但是注释本身中的字节实际上是被读入的,然后在解析期间被跳过。这意味着,如果您有大量的评论(例如数百兆字节),这将减慢解释器的速度。但是,这也会减慢任何编译器的速度。
#10
0
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.
我想知道如何使用评论。例如,三重引号是文档字符串。如果您使用它们,则会验证内容。我在一段时间内遇到了一个问题,我将一个库导入我的Python 3代码......我在\ N上遇到了关于语法的错误。我查看了行号,它是三重引用评论中的内容。我有点惊讶。 Python新手,我从未想过会因语法错误而解释块注释。
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
Python 2不会抛出错误,但Python 3报告:SyntaxError :( unicode error)'unicodeescape'编解码器无法解码位置14-15中的字节:格式错误\ N字符转义
So Python 3 is evidently interpreting the triple quote, making sure it's valid syntax.
所以Python 3显然是在解释三重引号,确保它是有效的语法。
However, if turned into a single line comment: # (i.e. \Device\NPF_..)
No error results.
但是,如果转换为单行注释:#(即\ Device \ NPF_ ..)没有错误结果。
I wonder if the triple quote comments wer replaced with single lines, if a performance change would be seen.
如果可以看到性能变化,我想知道三重引用评论是否被单行代替。