在 R system.time(exp) 输出中测量的“用户”和“系统”时间是多少?

Posted

技术标签:

【中文标题】在 R system.time(exp) 输出中测量的“用户”和“系统”时间是多少?【英文标题】:What are 'user' and 'system' times measuring in R system.time(exp) output? 【发布时间】:2011-08-07 00:47:28 【问题描述】:

我正在使用system.time(expression) 来测量 R 函数的执行时间。

我得到的调用输出

system.time(myfunction())

是:

    user  system elapsed   
  117.36    5.65  127.86

“用户”和“系统”衡量什么?

【问题讨论】:

en.wikipedia.org/wiki/Time_(Unix)#User_Time_vs_System_Time 这个问题可以使用更好的标题——例如“什么是‘用户’和‘系统’时间测量?”。这将使浏览列表的人更清楚问题。 【参考方案1】:

这在?proc.time 中进行了讨论(system.time() 返回类"proc.time" 的对象):

Details:

     ‘proc.time’ returns five elements for backwards compatibility, but
     its ‘print’ method prints a named vector of length 3.  The first
     two entries are the total user and system CPU times of the current
     R process and any child processes on which it has waited, and the
     third entry is the ‘real’ elapsed time since the process was
     started.

....和

Value:

....

     The definition of ‘user’ and ‘system’ times is from your OS.
     Typically it is something like

     _The ‘user time’ is the CPU time charged for the execution of user
     instructions of the calling process. The ‘system time’ is the CPU
     time charged for execution by the system on behalf of the calling
     process._

【讨论】:

【参考方案2】:

usersystem 已用时间之间的区别我读过的最清晰的解释是由 William Dunlap on [R-help] 提供的:

“用户CPU时间”给出当前进程所花费的CPU时间 (即当前 R 会话)和“系统 CPU 时间”给出 CPU 内核(操作系统)代表 当前进程。操作系统用于诸如 打开文件,进行输入或输出,启动其他进程,以及 查看系统时钟:涉及资源的操作 许多进程必须共享。

虽然?proc.time 返回了类似的内容,但这个描述对我来说更容易理解。

【讨论】:

【参考方案3】:

这里有一些简单的解释:

Elapsed Time 是为表达式计费到 CPU 的时间。

用户时间是挂钟时间。您作为用户所经历的时间。

通常这两个时间都比较接近。但在其他一些情况下,它们可能会有所不同。例如:

如果 经过的时间 > 用户时间,这意味着 CPU 正在等待其他一些操作(可能是外部的)完成。 如果经过时间,这意味着您的机器有多个内核并且能够使用它们

【讨论】:

【参考方案4】:

因为这些都是通用的,来自***:

术语“用户 CPU 时间”可能有点 起初误导。需要明确的是, 总时间(实际 CPU 时间)是 时间量的组合 CPU 花费在执行某些操作上 一个程序和时间量 CPU 用于执行系统调用 代表程序的内核。 当程序循环遍历数组时, 它正在累积用户 CPU 时间。 相反,当一个程序执行一个 系统调用,例如 exec 或 fork,它 正在累积系统 CPU 时间。

http://en.wikipedia.org/wiki/Time_(Unix)#User_Time_vs_System_Time

【讨论】:

【参考方案5】:

由于这些时间变量是由您的操作系统定义的,您可以通过在 shell(在 Unix 上)中执行 man time 来检索有关如何计算它们的信息:

...这些统计数据包括 (i) 调用和终止之间经过的实时时间,(ii) 用户 CPU 时间(由 times 返回的 struct tms 中 tms_utimetms_cutime 值的总和(2)) 和 (iii) 系统 CPU 时间(times(2) 返回的结构 tms 中 tms_stimetms_cstime 值的总和)。

上述时间变量的定义可以是found here:

tms_utime 用户 CPU 时间。

tms_stime 系统 CPU 时间。

tms_cutime已终止子进程的用户 CPU 时间。

tms_cstime已终止子进程的系统 CPU 时间。

daroczig 的回答和elsewhere on SO 中描述了用户和系统时间之间差异的说明:

tms_utime 元素是执行代码或 C 库中的代码所花费的时间。 tms_stime 元素是内核代表您执行代码所花费的时间。

【讨论】:

以上是关于在 R system.time(exp) 输出中测量的“用户”和“系统”时间是多少?的主要内容,如果未能解决你的问题,请参考以下文章

R - 使用 System.time 获取当前日期,并将时间更改为 timevis 时间线的固定值

R语言使用system.time函数统计多个函数运行的累加(累计)时间计算h2o包生成的多个算法模型进行特征重要度分析累计耗费的时间

R语言使用system.time函数统计多个函数运行的累加(累计)时间计算h2o包生成的多个算法模型进行特征重要度分析累计耗费的时间

将输出外壳转换为 JSON

是否可以计算 R 中的算术运算次数?

在python中执行linux time命令