我怎样才能加速以下循环?R加速while循环
count <- function(start, stepsize, threshold) {
i <- 1;
while (start <= threshold) {
start <- stepsize*i+start;
i <- i+1;
}
return(i-1);
}
system.time(count(1, 0.004, 1e10))
我怎样才能加速以下循环?R加速while循环
count <- function(start, stepsize, threshold) {
i <- 1;
while (start <= threshold) {
start <- stepsize*i+start;
i <- i+1;
}
return(i-1);
}
system.time(count(1, 0.004, 1e10))
工作出款项在上面的评论:
## start + S*n*(n-1)/2 = T
## (T-start)*2/S = n*(n-1)
## n*(n-1) - (T-start)*2/S = 0
的函数来解决这个二次方程:
ff <- function(start,stepsize,threshold) {
C <- (threshold-start)*2/stepsize
ceiling((-1 + sqrt(1+4*C))/2)
}
该解决方案基本上不花时间......
> system.time(cc <- count(1, 0.004, 1e10))
user system elapsed
5.372 0.056 5.642
> system.time(cc2 <- ff(1, 0.004, 1e10))
user system elapsed
0 0 0
> cc2
[1] 2236068
> cc
[1] 2236068
该曲关键是这是否能够推广到您需要解决的确切问题。
它看起来像你试图做到这一点:
recount <- function(start, stepsize, threshold) {
NewCount <<- floor((threshold-start)/stepsize)
}
(fast <- system.time(recount(1, 0.004, 1e10)))
它不带任何可测量的时间。
没有全局变量,这里是什么样子:
recount <- function(start, stepsize, threshold) {
return(floor((threshold-start)/stepsize))
}
(fast <- system.time(NewCount <- recount(1, 0.004, 1e10)))
??但是这并没有给出与OP的测试代码相同的答案......并且设置一个全局变量而不是返回一个值是相当单一的... – 2012-01-12 16:13:44
有一个有趣的博客如何加快R中循环使用的一些技巧
Another aspect of speeding up loops in R
这是例如在该页面报告
NROW=5000
NCOL=100
#Ex. 1 - Creation of a results matrix where its memory
#allocation must continually be redefined
t1 <- Sys.time()
x <- c()
for(i in seq(NROW)){
x <- rbind(x, runif(NCOL))
}
T1 <- Sys.time() - t1
#Ex. 2 - Creation of a results matrix where its memory
#allocation is defined only once, at the beginning of the loop.
t2 <- Sys.time()
x <- matrix(NA, nrow=NROW, ncol=NCOL)
for(i in seq(NROW)){
x[i,] <- runif(NCOL)
}
T2 <- Sys.time() - t2
#Ex. 3 - Creation of a results object as an empty list of length NROW.
#Much faster than Ex. 1 even though the size of the list is
#not known at the start of the loop.
t3 <- Sys.time()
x <- vector(mode="list", NROW)
for(i in seq(NROW)){
x[[i]] <- runif(NCOL)
}
T3 <- Sys.time() - t3
png("speeding_up_loops.png")
barplot(c(T1, T2, T3), names.arg = c("Concatenate result", "Fill empty matrix", "Fill empty list"),ylab="Time in seconds")
dev.off()
T1;T2;T3
对于这个特定的问题,我会制定出手工加总 - 例如你知道从1到n的和(i)是n *(n + 1) - 然后求解适当的二次方程并进行调整。你也可以字节编译......是更大问题的一部分,还是只需要解决这个确切的问题? – 2012-01-08 15:17:19
就是这样!非常感谢! – rua 2012-01-08 16:27:28