如何记录一个程序在其他程序运行时的实际运行时间?

如何记录一个程序在其他程序运行时的实际运行时间?

当其他程序正在运行时,在 Linux 上对 OpenMP 程序的 wall time 性能进行计时时,如何获得实际的运行时间?

答案1

称呼time myprogram

这会报告挂钟时间、用户时间和系统时间。用户时间是进程在计算上花费的时间。如果程序是多线程的并且机器具有多个处理器,则将所有处理器上花费的时间相加(因此对于充分并行的程序,用户时间可能比挂钟时间更长)。系统时间是在内核中花费的时间,即进行输入/输出。

这与“不计算其他正在运行的程序的干扰的时间”非常接近。如果没有并发程序,则了解该程序将花费多少挂钟时间的唯一方法是在没有其他并发程序的情况下运行该程序。

答案2

如果你能得到pid,这对任何一个都不难ps/proc/self或者$!取决于你是否背景它,你可以在以下位置找到它:

/proc/$pid/stat:

          utime %lu   (14) Amount of time that this process has been
                      scheduled in user mode, measured in clock ticks
                      (divide by sysconf(_SC_CLK_TCK)).  This includes
                      guest time, guest_time (time spent running a
                      virtual CPU, see below), so that applications that
                      are not aware of the guest time field do not lose
                      that time from their calculations.

          stime %lu   (15) Amount of time that this process has been
                      scheduled in kernel mode, measured in clock ticks
                      (divide by sysconf(_SC_CLK_TCK)).

          cutime %ld  (16) Amount of time that this process's waited-for
                      children have been scheduled in user mode,
                      measured in clock ticks (divide by
                      sysconf(_SC_CLK_TCK)).  (See also times(2).)  This
                      includes guest time, cguest_time (time spent
                      running a virtual CPU, see below).

          cstime %ld  (17) Amount of time that this process's waited-for
                      children have been scheduled in kernel mode,
                      measured in clock ticks (divide by
                      sysconf(_SC_CLK_TCK)).

要获取进程 ID,您可以执行以下操作:

prog ./and/args &
pid=$!

{ prog ./and/args & true ; } && ps -C prog

prog ./and/args

CTRL-Z

jobs -l ; fg %1

有很多方法。

相关内容