【问题标题】:How to measure both CPU and GPU time taken for each line for python code?如何测量 Python 代码每行所花费的 CPU 和 GPU 时间?
【发布时间】:2020-02-06 01:45:47
【问题描述】:

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

from timeit import default_timer as timer

start = timer()
# ...
end = timer()
print(end - start)

【问题讨论】:

  • 您的意思是对您的代码进行时间分析吗?我是从 C/C++ 的 valgrind 角度思考而来的

标签: python time gpu cpu


【解决方案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 获取状态,我没有挖掘他们的文档,但他们在 README.md 中提到了 time 库支持。

【讨论】:

    猜你喜欢
    • 2019-06-10
    • 2013-12-11
    • 2015-02-15
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多