r - 计算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)
是否有其他替代方案,或者我可以根据需要更新这些功能
解决方案
根据您对“复杂性”的想法,您有很多选择,仅举几例:
圈复杂度
- 通过程序源代码的线性独立路径数量的定量测量 - 例如由 cyclocomp 包实现:https ://cran.r-project.org/web/packages/cyclocomp/index.html
内存使用
- Base R 提供了一个选项来分析内存使用(如果 R 是使用定义的 R_MEMORY_PROFILING 编译的)使用
Rprofmem
- 纯内存使用分析器。 - 采样内存分析
utils::Rprof(..., memory.profiling = TRUE)
- profvis - Rprof 的包扩展,对于经过的时间和内存都有非常好的图形输出 - https://cran.r-project.org/web/packages/profvis/index.html
所花费的时间
- 用许多方法来测量所花费的时间,例如,基函数
system.time
返回两次调用之间的差异,proc.time
在该调用中它计算作为参数提供的表达式。最简单的用法:system.time(runif(10^8))
- 时间分析包,例如https://cran.r-project.org/package=microbenchmark或https://cran.r-project.org/package=bench
推荐阅读
- python - 无法使用 BeautifulSoup find_all 或 Pandas.read_html 函数从表中抓取数据
- python - 如何从 jupyter notebook 运行 tf.test.TestCase - UnrecognizedFlagError: Unknown command line flag 'f'
- office-js - 强制 Office 2016 加载项使用 Edge 代替 IE
- reactjs - 在打字稿中设置反应类组件状态的默认值
- java - 将 Spring Cloud 版本从 Edgware 升级到 Hoxton
- python - 使用 for 循环打印名称
- rust - 为什么弧
> 需要 T:同步 + 发送? - macos - 如何在 mac 上安装 clang 11
- functional-programming - 无法在 agda 中安装默认库
- python - 将列从对象转换为浮点数时出现值错误