下面的代码计算整个python代码占用的CPU时间。如何将其扩展到每一行代码并增加GPU时间?
from timeit import default_timer as timer
start = timer()
# ...
end = timer()
print(end - start)
您可以通过以下方式跟踪CPU时间:
import time
time_collect = list()
start = time.process_time()
# single line code here
time_collect.append(time.process_time() - start)
# next single code here
time_collect.append(time.process_time() - start)
# next single code here
time_collect.append(time.process_time() - start)
# This line will give you the time for every step after which you append the time
steps_time = [time_collect[i+1] - time_collect[i] for i in range(len(time_collect) - 1)]
[如果有GPU,则可能需要GPUtil才能从GPU的状态中获取信息,我没有挖掘他们的文档,但是他们在time
中提到了README.md
库支持。