使用 Sys.time() 计时 R 代码 [英] Timing R code with Sys.time()
问题描述
我可以使用以下代码运行一段代码 5 或 10 秒:
I can run a piece of code for 5 or 10 seconds using the following code:
period <- 10 ## minimum time (in seconds) that the loop should run for
tm <- Sys.time() ## starting data & time
while(Sys.time() - tm < period) print(Sys.time())
代码可以正常运行 5 或 10 秒.但是当我用 60 替换周期值让它运行一分钟时,代码永远不会停止.怎么了?
The code runs just fine for 5 or 10 seconds. But when I replace the period value by 60 for it to run for a minute, the code never stops. What is wrong?
推荐答案
只要经过的时间超过 1 分钟,默认单位就会从秒变为分钟.所以你想控制单位:
As soon as elapsed time exceeds 1 minute, the default unit changes from seconds to minutes. So you want to control the unit:
while (difftime(Sys.time(), tm, units = "secs")[[1]] < period)
来自 ?difftime
If ‘units = "auto"’, a suitable set of units is chosen, the
largest possible (excluding ‘"weeks"’) in which all the absolute
differences are greater than one.
Subtraction of date-time objects gives an object of this class, by
calling ‘difftime’ with ‘units = "auto"’.
或者使用 proc.time
,它可以在几秒钟内测量自您启动 R 会话以来的各种时间(用户"、系统"、已用时间").我们想要已用"时间,即挂钟时间,因此我们检索 proc.time()
的第三个值.
Alternatively use proc.time
, which measures various times ("user", "system", "elapsed") since you started your R session in seconds. We want "elapsed" time, i.e., the wall clock time, so we retrieve the 3rd value of proc.time()
.
period <- 10
tm <- proc.time()[[3]]
while (proc.time()[[3]] - tm < period) print(proc.time())
如果您对[[1]]
和[[3]]
的使用感到困惑,请咨询:
If you are confused by the use of [[1]]
and [[3]]
, please consult:
让我添加一些用户友好的可重现示例.您在循环内带有 print
的原始代码非常烦人,因为它会在屏幕上打印数千行.我会使用 Sys.sleep
.
Let me add some user-friendly reproducible examples. Your original code with print
inside a loop is quite annoying as it prints thousands of lines onto the screen. I would use Sys.sleep
.
test.Sys.time <- function(sleep_time_in_secs) {
t1 <- Sys.time()
Sys.sleep(sleep_time_in_secs)
t2 <- Sys.time()
## units = "auto"
print(t2 - t1)
## units = "secs"
print(difftime(t2, t1, units = "secs"))
## use '[[1]]' for clean output
print(difftime(t2, t1, units = "secs")[[1]])
}
test.Sys.time(5)
#Time difference of 5.005247 secs
#Time difference of 5.005247 secs
#[1] 5.005247
test.Sys.time(65)
#Time difference of 1.084357 mins
#Time difference of 65.06141 secs
#[1] 65.06141
自动"单位非常聪明.如果sleep_time_in_secs = 3605
(超过一个小时),默认单位将更改为小时".
The "auto" units is very clever. If sleep_time_in_secs = 3605
(more than an hour), the default unit will change to "hours".
在使用 Sys.time
时要小心时间单位,否则您可能会在基准测试中上当.这是一个完美的例子:read.csv/fread 基准测试中的意外结果.我已经用现在删除的评论回答了它:
Be careful with time units when using Sys.time
, or you may be fooled in a benchmarking. Here is a perfect example: Unexpected results in benchmark of read.csv / fread. I had answered it with a now removed comment:
您的时间单位有问题.我看到 fread
快了 20 多倍.如果 fread
需要 4 秒读取一个文件,read.csv
需要 80 秒 = 1.33 分钟.忽略单位,read.csv
更快".
You got a problem with time units. I see that
fread
is more than 20 times faster. Iffread
takes 4 seconds to read a file,read.csv
takes 80 seconds = 1.33 minutes. Ignoring the units,read.csv
is "faster".
现在让我们测试 proc.time
.
test.proc.time <- function(sleep_time_in_secs) {
t1 <- proc.time()
Sys.sleep(sleep_time_in_secs)
t2 <- proc.time()
## print user, system, elapsed time
print(t2 - t1)
## use '[[3]]' for clean output of elapsed time
print((t2 - t1)[[3]])
}
test.proc.time(5)
# user system elapsed
# 0.000 0.000 5.005
#[1] 5.005
test.proc.time(65)
# user system elapsed
# 0.000 0.000 65.057
#[1] 65.057
用户"时间和系统"时间为0,因为CPU和系统内核都空闲.
"user" time and "system" time are 0, because both CPU and the system kernel are idle.
这篇关于使用 Sys.time() 计时 R 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!