time.process_time() 打印 'e-xx'

time.process_time() printing 'e-xx'

我正在记录执行每项功能所需的时间长度。

计算成功。但是,有一半时间 print 包含 e-xx (xx - 数字)。即使将我的答案设为 float().

import time

tic = time.process_time()
a = 1 + 2
print(a)
toc = time.process_time()
print(str(round(float(toc - tic), 8)) + "s")
>>> 3
>>> 4.57e-06s

要强制输出匹配给定格式,例如 MWE 中小数点后有 8 位的浮点数,请使用 str.format()。将第二个 print 命令替换为

print('{:.8f}s'.format(toc - tic))

将给出所需的输出。就我而言 0.00020080s。有关字符串格式化语法的详细信息,请参阅 this page