There are a couple ways to benchmark with base R:
.t0 <- Sys.time()
...
.t1 <- Sys.time()
.t1 - t0
# or
system.time({
...
})
With the Sys.time
way, you're sending each line to the console and may see some return value printed for each line, as @Axeman suggested. With {...}
, there is only one return value (the last result inside the braces) and system.time
will suppress it from printing.
If the printing is costly enough but is not part of what you want to measure, it can make a difference.
There are good reasons to prefer system.time
over Sys.time
for benchmarking; from @MattDowle's comment:
i) it does a gc first excluded from the timing to isolate from random gc's and
ii) it includes user
and sys
time as well as elapsed
wall clock time.
The Sys.time()
way will be affected by reading your email in Chrome or using Excel while the test runs, the system.time()
way won't so long as you use the user
and sys
parts of the result.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…