如何测量python代码每一行的CPU和GPU时间?

问题描述 投票:1回答:1

下面的代码计算整个python代码占用的CPU时间。如何将其扩展到每一行代码并增加GPU时间?

from timeit import default_timer as timer

start = timer()
# ...
end = timer()
print(end - start)
python time gpu cpu
1个回答
1
投票

您可以通过以下方式跟踪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库支持。

© www.soinside.com 2019 - 2024. All rights reserved.