2011-04-16 69 views
67

我正在使用system.time(expression)来测量R函数的执行时间。在R system.time(exp)输出中测量的“用户”和“系统”时间是什么?

我得到呼叫

system.time(myfunction()) 

输出是:

user system elapsed 
    117.36 5.65 127.86 

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

+1

http://en.wikipedia.org/wiki/Time_(Unix)#User_Time_vs_System_Time – manojlds 2011-04-16 19:28:35

+0

这个问题可以使用更好的标题 - 比如“什么是用户”和“系统”时间测量?“。这会让浏览列表的人更清楚。 – Sharpie 2011-04-17 05:39:09

回答

35

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

由于这些是通用的,无论如何,从百科:

术语'用户CPU时间'可能会有点误导 首先。需要明确的是,在 总时间(实际CPU时间)的时间 CPU花费进行了 程序一些动作和时间的量量的 组合 CPU花费在执行系统调用 内核上代表该计划。 当一个程序在一个数组中循环时,它正在累积用户CPU时间。 相反,当程序执行系统调用(如exec或fork)时, 正在累计系统CPU时间。

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

34

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

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

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

0

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

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

2

下面是一些简单的解释:

执行时间被充电到CPU(一个或多个),用于表达的时间。

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

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

  • 如果经过时间>用户时间,这意味着CPU为一些其它操作等待周围(可以是外部的)工作要做。
  • 如果经过时间<用户时间,这意味着你的机器有多个内核,并能够使用它们
相关问题