简体   繁体   中英

Measure the cpu usage/execution time for tesorflow in python

I want to compare two tensorflow programs, my assumption is that one of program will have lower cpu usage. I am not so sure whether I should use time.clock() or time.time(). I currently uses 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)

I think that 'time.time()' will measure the execution time which can also be considered as latency (one thread might wait for another thread), and the 'time.clock()' will measure the cpu time (the total cpu time, without wait time between threads).

If I want to know whether my program will have lower cpu usage or not, I should focus on the 'time.clock()', right?

Another question is whether 'time.clock()' will correctly measure the cpu time of a Tensorflow program or not?

You can use psutil to get the cpu usage, memory usage, disk usage etc. from python. It's really simple. Here is an example.

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.

They have good documentation, check out their docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM