首页 > 解决方案 > python程序中多个片段的CPU时间计数

问题描述

我正在编写代码来计算程序的 CPU 时间。相关代码是这样的:

   with open(script_path, "r") as src:
        scr_contents = src.read()

   proc = subprocess.run(["python", script_path], capture_output = True)

   cup_time = time.process_time()

这个脚本给了我运行整个 scr_contents 程序的 CPU 时间。

现在,假设我在这个程序中有很多片段,并且我想获取每个片段的 CPU 时间,有什么方法或函数可用于这个 CPU 时间计数的目的?

标签: pythontimecpu-usage

解决方案


推荐阅读