小编신길선*_*신길선的帖子

为什么一行"for"表达式在Python中更快?

我测试了四种情况.

1.

testList = list()
for i in range(1000000):
    testList.append(i)
Run Code Online (Sandbox Code Playgroud)

2.

testList = list()
for i in range(1000000): testList.append(i)
Run Code Online (Sandbox Code Playgroud)

3.

testList = list()
newAppend = testList.append
for i in range(1000000): newAppend(i)
Run Code Online (Sandbox Code Playgroud)

4.

[i for i in range(1000000)]
Run Code Online (Sandbox Code Playgroud)

每次都是

1:0.09166

2:0.08299

3:0.05003

4:0.04594

为什么其他人比1快?

我能找到相关的关键词吗?

python python-2.7

5
推荐指数
0
解决办法
716
查看次数

标签 统计

python ×1

python-2.7 ×1