Skip to main content
 首页 » 编程设计

python之为什么我的程序使用系统 cpu 时间

2025年04月02日17langtianya

我写了一个耗时的python程序。基本上,python 程序大部分时间都在 f2py 包装的 fortran 例程中度过,而 fortran 例程大部分时间都在 lapack 中度过。然而,当我在我的工作站上运行这个程序时,我发现 80% 的 cpu 时间是用户时间,20% 的 cpu 时间是系统时间。

在另一个 SO 问题中,我读到:

The difference is whether the time is spent in user space or kernel space. User CPU time is time spent on the processor running your program's code (or code in libraries); system CPU time is the time spent running code in the operating system kernel on behalf of your program.

所以如果这是真的,我假设所有的 cpu 时间都应该用于用户时间。 20% 的系统时间是否表示我需要分析程序?

编辑:

更多信息:我无法重现 20% 的系统 CPU 时间。在另一次运行中,time 命令给出:

  • 真正的 5m14.804s
  • 用户 78m6.233s
  • 系统 4m53.896s

请您参考如下方法:

操作系统在任何给定时刻不断地切换正在运行的内容。你的程序会运行一段时间,但最终会出现中断,系统会切换到其他程序,或者它可能只是决定运行其他程序一两秒钟,然后再切换回去。很难强制操作系统不执行此行为。这是操作系统工作的一部分;让所有领域的事物都在移动。