代码简单
@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 …