欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

测试python程序运行时间

程序员文章站 2022-05-27 15:11:36
...
import time
import decimal

# text decimal_sum
print("decimal:")
start =time.time()

sum = decimal.Decimal('0')
for x in range(100):
    sum += decimal.Decimal(x)

end = time.time()
# print('Running time: %s Seconds'%(end-start))

print('Running time: {} Seconds'.format(end-start))
print("===============================")
#text_int
print("sum_text")
start =time.time()
sum = 0

for x in range(100):
    sum += x

end = time.time()
print('Running time: {} Seconds'.format(end-start))
print("===============================")
#print("int")
print("int")
start =  time.time()

print(4.0)
end = time.time()
print('Running time: {} Seconds'.format(end-start))
print("===============================")

print("str")
start =  time.time()

print("sdfgh")
end = time.time()
print('Running time: {} Seconds'.format(end-start))
print("===============================")

测试python程序运行时间

经由测试,print()花费时间大约是1e-6s,decimal运行花费的时间大约是int的三倍。