Fan*_*nto 0 python optimization cpython compilation
有人可以向我解释为什么 CPython 没有像循环不变优化这样的循环优化吗?例如:
for i in range(100000000000):
pass
Run Code Online (Sandbox Code Playgroud)
cpython 不会跳过循环并需要一些时间来完成执行;这也会发生在 while 循环中。
因为 python 不知道什么range是直到你运行循环,因为内置函数可以被覆盖。如果我做
class range:
def __init__(self, n):
self.n = n
global x
x = 0
def __iter__(self):
global x
while x < self.n:
yield x
x += 1
for i in range(20): pass
print(x) # prints 20
Run Code Online (Sandbox Code Playgroud)
你可以看到在动态语言中很难优化这样的东西
| 归档时间: |
|
| 查看次数: |
203 次 |
| 最近记录: |