检查 R 代码中哪个 row/function 在函数中花费的时间最多?

Examine which row/function in R code takes the most time within function?

我有一组函数 运行 在包装器中:

wrapper_func <- function(x,y,z,.....) {

t <- foo1(x,y)
kuku <- foo2(t,z)
....
final_res <- foo20(t, kuku, ...)

return(final_res)

}

它 运行 很慢,我想了解 bottleneck/troublemaker 是谁。 请告知哪个函数可以执行更深入的分析(基准测试?微基准测试?...)以显示向下钻取 - row/function 占用最多 time/resources?

我又发现了一个option

并转到使用 lineprof 进行内存分析一章。

你怎么看?

您可以使用 Rprof 分析您的 R 代码并找出性能瓶颈;这是一个简短的例子

tmp <- tempfile()
Rprof(tmp)
example(glm)
Rprof()
summaryRprof(tmp)

可以在 this R-bloggers article.

找到更详细的描述

您可以打印 microbenchmark 结果以查看您的性能瓶颈:

library(microbenchmark)

wrapper_func <- function(x,y,z,.....) {

  t <- foo1(x,y)
  print(microbenchmark(foo1(x,y)))
  kuku <- foo2(t,z)
  print(microbenchmark(foo2(t,z)))
  ....
  final_res <- foo20(t, kuku, ...)
  print(microbenchmark(foo20(t, kuku, ...)))

  return(final_res)

}