这是一个通用问题。
我写了一段代码,使用 tensorflow 进行计算。
我想打印代码执行过程中消耗的时间。
我首先使用:
import time
start = time.time()
main()
print ("%s" % (time.time() - start_time))
但我读到这是一种不准确的执行时间测量方法。
如何准确测量我的程序的执行时间。
最佳答案
使用 time.<strong>perf_counter</strong>()
. perf_counter
是一个“性能计数器”。它是从平台上可用的未定义起点(通常是从程序开始运行起)开始的最高分辨率时间。它用于衡量从后续调用中减去时的性能。它是几秒钟的 float 。
time.<strong>time</strong>()
是自 Unix 纪元(1970 年 1 月 1 日)以来的秒数,可能不会比秒更精确。
您可以安全地替换对 time.time()
的调用与 time.perf_counter()
,因为您正在减去它们。
import time
start = time.perf_counter()
main()
elapsed = time.perf_counter() - start
print('Elapsed %.3f seconds.' % elapsed)
# The .3f is to round to 3 decimal places.
关于python - 如何打印在 tensorflow 代码中完成的计算的执行时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41816017/