我使用Rprof来分析代码并重写慢速部分。但是,我在Rprof的报告中遇到了一个奇怪的功能:
> summaryRprof()
$by.self
self.time self.pct total.time total.pct
"eval" 58.34 76.06 76.70 100.00
".C" 8.38 10.93 8.38 10.93
"is.null" 3.42 4.46 3.42 4.46
...
我检查了Rprof的输出文件,我看到了很多
"eval" "eval" "withVisible" "source"
"eval" "eval" "withVisible" "source"
"eval" "eval" "withVisible" "source"
"eval" "eval" "withVisible" "source"
"eval" "eval" "withVisible" "source"
"eval" "eval" "withVisible" "source"
"eval" "eval" "withVisible" "source"
"eval" "eval" "withVisible" "source"
"eval" "eval" "withVisible" "source"
"eval" "eval" "withVisible" "source"
"eval" "eval" "withVisible" "source"
那么,这是什么以及如何改进代码?
以下是代码的主要部分,它是抽样,并根据不同的组策略来估算一个数字。我使用嵌套列表来执行此操作。相当尴尬。
Rprof(file)
for (iter in 1 : 10) {
tmp_samples <- sample(x = 1 : total_lost, size = sample_number, replace = TRUE)
for (ele in tmp_samples) {
cell_id <- (ele + 2) %/% 3
for (i in 1 : nrow(cluster)) {
area <- cluster[i, cell_id]
groups[[i]][[area]] <- c(groups[[i]][[area]], ele)
}
for (i in 1 : nrow(cluster))
for (j in 1 : group_num[i])
if (!is.null(groups[[i]][[j]]))
.C("estimate",
as.integer(groups[[i]][[j]]),
as.integer(length(groups[[i]][[j]])),
as.double(original[i, j]), as.double(smoothed[i, j]))
}
}
Rprof(NULL)