【问题标题】:Profiling performance of functions that call other functions分析调用其他函数的函数的性能
【发布时间】:2012-11-04 10:25:57
【问题描述】:

我正在编写一个相对复杂的函数来进行从头到尾的数据分析。该函数调用了我编写的许多子函数(它们本身称为子子函数等)。我正在寻找一种工具来告诉我我的功能在每个子功能和子子功能上花费了多少时间,以便了解我应该在哪里寻找性能改进。类似于 MATLAB 的 profiler 的东西会很好。

例如,一个假设函数audit

#fun1 is called by fun2
fun1 <- function() {
  rnorm(100000)
}

fun2 <- function(x) {
  y <- x+1
  z <- y*fun1()
}

mainFun <- function() {
  z+3
}

audit(mainFun())
> mainFun = 1 s; of which 95% is in fun1 and 98% is fun2

显然我可以为每个单独的函数使用microbenchmark()system.time() - 但是随着mainFun 变得更加复杂,有效地使用它变得很棘手。有现成的工具吗?

【问题讨论】:

    标签: performance r profiling


    【解决方案1】:

    搜索R profiler 将引导您找到Rprof 函数,这正是您要寻找的。本质上,您调用Rprof() 并正常运行您的脚本。 summaryRprof 函数是总结分析器结果的便捷方式。有关更多详细信息,请查看?RprofWriting R Extensionsthis linkthis SO question 甚至this set of SO questions 中关于Tidying and profiling R code 的章节。

    【讨论】:

    • 这里也对 R 分析进行了一些很好的讨论:stackoverflow.com/questions/3650862/…
    • + 我用过rprof,我很高兴(这有点)。我不关心摘要工具。我只是抓取包含堆栈跟踪的结果文件并检查它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    相关资源
    最近更新 更多