我正在使用system.time(expression)
来测量R函数的执行时间。
对于这个调用,我得到的输出是
system.time(myfunction())
是:
user system elapsed
117.36 5.65 127.86
What does 'user' and 'system' measure?
我正在使用system.time(expression)
来测量R函数的执行时间。
对于这个调用,我得到的输出是
system.time(myfunction())
是:
user system elapsed
117.36 5.65 127.86
What does 'user' and 'system' measure?
user
和 system
运行时间差异的最清晰解释,这是由 William Dunlap 在 [R-help] 上提供的:
尽管"用户 CPU 时间" 是指当前进程(即当前 R 会话)使用的 CPU 时间,“系统 CPU 时间” 是指操作系统内核(kernel)代表当前进程使用的 CPU 时间。操作系统用于打开文件、输入或输出、启动其他进程以及查看系统时钟等操作:这些操作涉及许多进程必须共享的资源。
?proc.time
返回了类似的信息,但对我来说,这个描述更容易理解。这个问题在?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._
这里有些简单的解释:
已过时间 是表达式被CPU(s)计算的时间。
用户时间 是指墙上的时钟时间,即作为用户所经历的时间。
通常这两个时间相对接近。但在其他情况下可能会有所不同。例如:
以下来自维基百科,由于这些都是通用术语,因此:
“用户CPU时间”一词可能一开始有点误导人。要明确的是,总时间(真实CPU时间)是CPU花费在为程序执行某些操作和代表程序为内核执行系统调用所花费的时间的组合。当程序循环遍历数组时,它正在累积用户CPU时间。相反,当程序执行像exec或fork这样的系统调用时,它正在累积系统CPU时间。
http://en.wikipedia.org/wiki/Time_(Unix)#User_Time_vs_System_Time
man time
来检索有关它们如何计算的信息:
tms_utime
元素是执行您的代码或C库中的代码所花费的时间。tms_stime
元素是在内核中代表您执行代码所花费的时间。