导入时间的脚本执行时间 --> 结果的单位是什么?

Script execution time for import time --> What units are the results in?

我正在使用 import time 模块来计算我的 while 循环的每个 运行 执行的时间。

代码如下:

while (...):
   start = time.time()

   ...code here

   end = time.time()
   
   print (f"python loop execution total time {end-start}")

上面的代码returns我:“python循环执行总时间2.3876123428344727”

这个 2.3876123428344727 值是以秒还是毫秒或其他形式表示的?我想我问的是代码 returns.

的时间单位

来自the documentation

Return the time in seconds since the epoch as a floating point number.

是 time.time() returns 浮点数的秒数。

详细说明,time.time() returns 当前时间戳。 时间戳是自 unix 时间开始以来经过的确切秒数,即 1970 年 1 月 1 日 00:00。

如果您想找出代码片段执行所花费的时间,您所做的并没有完全错误,但还有其他方法可以找到代码执行所花费的时间,这些方法更加标准化,例如 timeit 模块。

但对于简单的用例,您的方法应该足够好。