小编Jac*_* Xu的帖子

pypy3在10 ** 10循环时间内发生了什么

代码简单

@count_run_time
def test_while(l: int=0) -> (int, int):
    y = 0
    x = 0
    while x < l:
        y += x
        x += 1
    return x, y
Run Code Online (Sandbox Code Playgroud)

当我使用cpython(Python 3.6.8(v3.6.8:3c6b436a57,Dec 24 2018,02:04:31))运行时

test_while(10**5)
[func: test_while] cost @0.008665s
(100000, 4999950000)

test_while(10**6)
[func: test_while] cost @0.080222s
(1000000, 499999500000)

test_while(10**7)
[func: test_while] cost @0.814199s
(10000000, 49999995000000)

test_while(10**8)
[func: test_while] cost @7.944017s
(100000000, 4999999950000000)

test_while(10**9)
[func: test_while] cost @80.063558s
(1000000000, 499999999500000000)

test_while(10**10)
[func: test_while] cost @851.572578s
(10000000000, 49999999995000000000)
Run Code Online (Sandbox Code Playgroud)

从结果可以看出,随着循环次数的增加,消耗的时间也呈线性增加。

接下来,我尝试在pypy3(Python 3.6.1(784b254d6699,Apr 14 2019,10:22:55),[PyPy 7.1.1-beta0与GCC 4.2.1兼容Apple …

pypy python-3.x

5
推荐指数
1
解决办法
43
查看次数

标签 统计

pypy ×1

python-3.x ×1