from timeit import timeit
import math
import random
x = random.randint(9 ** 6, 9 ** 7)
y = random.randint(5 ** 5, 5 ** 6)
def with_math():
return math.gcd(x, y)
def with_euclid():
xx = x
yy = y
while yy:
xx, yy = yy, xx % yy
return xx
print(with_math() == with_euclid()) # Make sure result is same
print(timeit(with_math, number=10**7))
print(timeit(with_euclid, number=10**7))
您可以使用^{} 自己检查:)
运行以下命令:
在我当前的笔记本电脑上,它输出:
相关问题 更多 >
编程相关推荐