在python中测量tesorflow的cpu使用/执行时间

时间:2020-08-13 07:14:39

标签: python performance tensorflow

我想比较两个tensorflow程序,我的假设是其中一个程序的CPU使用率较低。我不确定应该使用time.clock()还是time.time()。我目前使用的是python2。

start = time.time()
for _ in range(100):
   sess.run(main.op)
end = time.time()
print((end - start)/100)

# or use clock to measure cpu time.
start = time.clock()
for _ in range(100):
   sess.run(main.op)
end = time.clock()
print((end - start)/100)

我认为'time.time()'将测量执行时间,也可以将其视为等待时间(一个线程可能等待另一个线程),而'time.clock()'将测量cpu时间(总的cpu时间,没有线程之间的等待时间。

如果我想知道我的程序是否会降低CPU使用率,我应该关注'time.clock()',对吗?

另一个问题是'time.clock()'是否可以正确测量Tensorflow程序的CPU时间?

1 个答案:

答案 0 :(得分:0)

您可以使用psutil从python获取CPU使用率,内存使用率,磁盘使用率等。真的很简单。这是一个例子。

import psutil

print(psutil.cpu_percent()) # for cpu usage
print(psutil.cpu_freq()) # for cpu frequncy
print(psutil.cpu_freq(True)) # for cpu frequency per core
print(psutil.disk_usage('/')) # for disk usage
print(psutil.virtual_memory()) # for RAM
print(psutil.sensors_temperatures()) # It might be CPU, Disk or other  hardware depending on config.

他们有很好的文档,请查阅他们的docs