2011-04-16 288 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爲一些其它操作等待周圍(可以是外部的)工作要做。
  • 如果經過時間<用戶時間,這意味着你的機器有多個內核,並能夠使用它們
相關問題