测试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("===============================")
经由测试,print()花费时间大约是1e-6s,decimal运行花费的时间大约是int的三倍。