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

时间:2011-04-16 19:21:49

标签: r time

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

我获得的电话输出

system.time(myfunction())

是:

    user  system elapsed   
  117.36    5.65  127.86

'用户'和'系统'的衡量标准是什么?

5 个答案:

答案 0 :(得分:43)

这在?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._

答案 1 :(得分:38)

我曾阅读过关于usersystem经过时间之间差异的最清晰解释,由William Dunlap on [R-help]提供:

  

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

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

答案 2 :(得分:16)

因为无论如何这些都是通用的,来自维基百科:

  

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

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

答案 3 :(得分:12)

以下是一些简单的解释:

已用时间 是表达式CPU的时间。

用户时间是挂钟时间。您作为用户体验的时间。

通常两次都相对接近。但在其他一些情况下它们可能会有所不同例如:

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

答案 4 :(得分:2)

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

  

...这些统计数据包括(i)调用和终止之间经过的实际时间,(ii)用户CPU时间(struct tms中tms_utimetms_cutime值的总和由times(2))返回,以及(iii)系统CPU时间(由时间(2)返回的struct 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元素是代表您在内核中执行代码所花费的时间。