gpt4 book ai didi

performance - 分析调用其他函数的函数的性能

转载 作者:行者123 更新时间:2023-12-02 05:24:05 26 4
gpt4 key购买 nike

我正在编写一个相对复杂的函数来进行从头到尾的数据分析。这个函数调用了很多我写的子函数(它们自己调用子子函数等)。我正在寻找一种工具来告诉我我的函数在每个子函数和子子函数上花费了多少时间,以便查看我应该在哪里寻找性能改进。类似于 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 变得更加复杂。有现成的工具吗?

最佳答案

谷歌搜索 R profiler 将引导您找到 Rprof 函数,这正是您要查找的内容。本质上,您调用 Rprof() 并正常运行您的脚本。 summaryRprof 函数是一种总结分析器结果的简便方法。有关详细信息,请查看 ?Rprof 和关于 Tidying and profiling R code 的章节在 Writing R Extensions , 或 this link , this SO question ,甚至 this set of SO questions .

关于performance - 分析调用其他函数的函数的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13403990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com