在R中是否有衡量函数执行时间的标准化方法?

显然我可以用系统。时间,然后取它们的差异,但我想知道是否有一些标准化的方法或功能(不想发明轮子)。


我似乎记得我曾经用过如下的东西:

somesysfunction("myfunction(with,arguments)")
> Start time : 2001-01-01 00:00:00  # output of somesysfunction
> "Result" "of" "myfunction"        # output of myfunction
> End time : 2001-01-01 00:00:10    # output of somesysfunction
> Total Execution time : 10 seconds # output of somesysfunction

当前回答

基于bench package网站:

bench::mark() from package bench is used to benchmark one or a series of expressions, we feel it has a number of advantages over alternatives. Always uses the highest precision APIs available for each operating system (often nanoseconds). Tracks memory allocations for each expression. Tracks the number and type of R garbage collections per expression iteration. Verifies equality of expression results by default, to avoid accidentally benchmarking inequivalent code. Has bench::press(), which allows you to easily perform and combine benchmarks across a large grid of values. Uses adaptive stopping by default, running each expression for a set amount of time rather than for a specific number of iterations. Expressions are run in batches and summary statistics are calculated after filtering out iterations with garbage collections. This allows you to isolate the performance and effects of garbage collection on running time (for more details see Neal 2014). The times and memory usage are returned as custom objects which have human readable formatting for display (e.g. 104ns) and comparisons (e.g. x$mem_alloc > "10MB"). There is also full support for plotting with ggplot2 including custom scales and formatting.

Use:

bench::mark(log10(5))
#> # A tibble: 1 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 log10(5)      212ns    274ns  2334086.        0B        0

由reprex包在2021-08-18创建(v2.0.1)

其他回答

基于bench package网站:

bench::mark() from package bench is used to benchmark one or a series of expressions, we feel it has a number of advantages over alternatives. Always uses the highest precision APIs available for each operating system (often nanoseconds). Tracks memory allocations for each expression. Tracks the number and type of R garbage collections per expression iteration. Verifies equality of expression results by default, to avoid accidentally benchmarking inequivalent code. Has bench::press(), which allows you to easily perform and combine benchmarks across a large grid of values. Uses adaptive stopping by default, running each expression for a set amount of time rather than for a specific number of iterations. Expressions are run in batches and summary statistics are calculated after filtering out iterations with garbage collections. This allows you to isolate the performance and effects of garbage collection on running time (for more details see Neal 2014). The times and memory usage are returned as custom objects which have human readable formatting for display (e.g. 104ns) and comparisons (e.g. x$mem_alloc > "10MB"). There is also full support for plotting with ggplot2 including custom scales and formatting.

Use:

bench::mark(log10(5))
#> # A tibble: 1 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 log10(5)      212ns    274ns  2334086.        0B        0

由reprex包在2021-08-18创建(v2.0.1)

包“tictoc”为您提供了一种非常简单的测量执行时间的方法。文档在:https://cran.fhcrc.org/web/packages/tictoc/tictoc.pdf。

install.packages("tictoc")
require(tictoc)
tic()
rnorm(1000,0,1)
toc()

要保存经过的时间到一个变量,你可以这样做:

install.packages("tictoc")
require(tictoc)
tic()
rnorm(1000,0,1)
exectime <- toc()
exectime <- exectime$toc - exectime$tic

还有proc.time()

你可以用和Sys一样的方法使用。Time但是它给你的结果和system。Time类似。

ptm <- proc.time()
#your function here
proc.time() - ptm

主要区别使用

system.time({ #your function here })

proc.time()方法仍然执行你的函数,而不是仅仅测量时间… 顺便说一下,我喜欢使用系统。时间{}在里面,所以你可以把一组东西…

library(rbenchmark)

sleep_func <- function() { Sys.sleep(0.5) }

benchmark(sleep_func())

out:

 test replications elapsed relative user.self sys.self user.child sys.child

1 sleep_func()          100   50.08        1      0.02        0         NA        NA

如果你喜欢,你可以使用matlab风格的tic-toc函数。看另一个SO问题

秒表功能在R