r - 追加(向量/列表)的时间复杂度是多少?
问题描述
https://www.programmingr.com/fast-r-append-list/
我没有发现对向量或列表进行附加操作的时间复杂度。
这在任何地方都有记录吗?
假设我有 n 个元素,我必须一个一个地追加。是否存在时间复杂度为 n 的数据结构来追加 n 个元素?
解决方案
这是一个函数,用于测试将值分配给向量的三种不同方式的运行时间。
前两个通过将新元素附加到其末尾来扩展向量。最后一个通过创建包含元素的向量来分配空间,n
然后用新元素填充向量。
用包装测量时间,用图形microbenchmark
绘制中位数。ggplot2
原子向量计时
fun <- function(N = 1:10, times = 100){
if(!require(microbenchmark)){
stop("Package 'microbenchmark' is not installed")
}
out <- lapply(2^N, function(n){
mb <- microbenchmark(
c = {
x <- NULL
for(i in seq_len(n)) x <- c(x, i)
},
append = {
x <- NULL
for(i in seq_len(n)) append(x, i)
},
allocate = {
x <- numeric(n)
for(i in seq_len(n)) x[i] <- i
},
times = times
)
mb$n <- n
aggregate(time ~ expr + n, mb, median)
})
out <- do.call(rbind, out)
row.names(out) <- NULL
out
}
library(ggplot2)
times <- fun(1:12)
ggplot(times, aes(n, time, color = expr)) +
geom_line() +
geom_point() +
theme_bw()
list
计时
这个函数与上面的函数几乎相同,但创建和扩展类“list”的对象。
令人惊讶的是,现在通过在当前最后一个元素之后分配新元素来扩展列表比append
.
funList <- function(N = 1:10, times = 100){
if(!require(microbenchmark)){
stop("Package 'microbenchmark' is not installed")
}
out <- lapply(2^N, function(n){
mb <- microbenchmark(
List = {
x <- list()
for(i in seq_len(n)) x[[i]] <- i
},
append = {
x <- list()
for(i in seq_len(n)) append(x, i)
},
allocate = {
x <- vector("list", n)
for(i in seq_len(n)) x[[i]] <- i
},
times = times
)
mb$n <- n
aggregate(time ~ expr + n, mb, median)
})
out <- do.call(rbind, out)
row.names(out) <- NULL
out
}
library(ggplot2)
times <- funList(1:12)
ggplot(times, aes(n, time, color = expr)) +
geom_line() +
geom_point() +
theme_bw()
推荐阅读
- c++ - CV4.1:函数detectAndCompute 级别>=0 中的断言失败
- python - Query PubMed with Python - 如何从查询中获取所有文章详细信息到 Pandas DataFrame 并以 CSV 格式导出
- java - 如何使用 portainer 名称作为主机名连接到 mongodb?
- python - 在 where 子句中使用字符串比较的 Python MySQL 查询
- javascript - 为什么在 HTML 内容中搜索时正则表达式不返回所有实例?
- laravel - laravel 中间件的所有 API 都不起作用
- ios - 如何在使用 UIPageControl 时调整 UIView 的大小以填充 UIScrollView 的内容
- sql - 如何在另一个表中查找缺少属性 ID 的记录 ID
- html - 在 xPath 中使用 AND 和 NOT
- bootstrap-4 - Parsley JS 不适用于引导选择