首页 > 解决方案 > 计算R中函数的复杂度

问题描述

我需要用 R 计算长函数(600 线)的复杂度。我想要的结果可能如下所示

start calculate complexity
**My function**
End calculate complexity
result= " the complexity of this function is x

我认为我想念理解但我在基准测试中发现的只是计算运行时间

start.time <- Sys.time()
*****function*****
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken

这个函数只适用于向量

    complexity(x, scaleMin, scaleMax, width = 7, measure = "complexity", 
   rescale = FALSE)

是否有其他替代方案,或者我可以根据需要更新这些功能

标签: rfunctiontime-complexitycomplexity-theory

解决方案


根据您对“复杂性”的想法,您有很多选择,仅举几例:

圈复杂度

内存使用

  • Base R 提供了一个选项来分析内存使用(如果 R 是使用定义的 R_MEMORY_PROFILING 编译的)使用Rprofmem- 纯内存使用分析器。
  • 采样内存分析utils::Rprof(..., memory.profiling = TRUE)
  • profvis - Rprof 的包扩展,对于经过的时间和内存都有非常好的图形输出 - https://cran.r-project.org/web/packages/profvis/index.html

所花费的时间


推荐阅读