python - 如何打印在 tensorflow 代码中完成的计算的执行时间?

标签 python tensorflow

这是一个通用问题。

我写了一段代码,使用 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/

相关文章:

python - 如何将 report_tensor_allocations_upon_oom 添加到 Keras 中的 RunOptions

tensorflow - 张量板不显示任何内容

python - 具有可变输入形状的 Keras

python - 具有三个特征的 LSTM 训练,但由于输入形状而无法预测

python - BeautifulSoup 查找多个类别

python - 在python3中读取和写入表格文本文件

python - 从 XBee 接收的 pySerial 数据未正确显示

python - Keras 中单个输入的多个输出向量

python - 如何在 django 模型中执行此 sql?

python - 从 Robot Framework 访问 python 类的变量