[R] decreasing performance of for() loop

From: bogdan romocea <br44114_at_gmail.com>
Date: Tue 11 Oct 2005 - 00:33:44 EST


Dear useRs,

I'm wondering why the for() loop below runs slower as it progresses. On a Win XP box, the iterations at the beginning run much faster than those at the end:

1%, iteration 2000, 10:10:16
2%, iteration 4000, 10:10:17
3%, iteration 6000, 10:10:17

98%, iteration 196000, 10:24:04
99%, iteration 198000, 10:24:24
100%, iteration 200000, 10:24:38

Is there something that can be done about this? Would such a loop run faster in C/C++/Fortran?

Thank you,
b.

#---sample code

loop.progress <- function(loop,iterations,steps,toprint=NULL) {
marks <- c(1,floor(iterations/steps)*(1:steps)) if (loop %in% marks) {

	if (is.null(toprint)) prt <- loop else prt <- toprint
	cat(paste(round((which(marks == loop)-1)*(100/steps),0),"%, iteration ",
		prt,", ",format(Sys.time(),"%H:%M:%S"),sep=""),"\n")
	}		

}
#---loop that runs slower and slower

test <- runif(200000)
out <- vector(mode="numeric")
lg <- 30
for (i in (lg+1):length(test))
	{
	loop.progress(i,length(test),100)	
	out[i] <- sum(test[(i-lg):i])
	}

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Oct 11 00:37:30 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:40:41 EST