I have nested for loops as below. I need p:q=1:300, and n=20. Function “mark” is the model of my interest(Package RMark). I know rbind can be slow but I have no idea what should be used to replace it. Otherwise what else I can do to make this function faster? Thanks.
foo<-function(data, p, q, n){
results.frame <- data.frame()
for (i in 1:n){
for (i in p:q) {
run.model<-mark(data[sample(nrow(data), i),], model="Occupancy")
results<-data.frame(summary(run.model)$real$p, Occupancy=summary(run.model)$real$Psi, se.p=t(as.matrix(summary(run.model, se=T)$real$p$se)), se.Psi=summary(run.model, se=T)$real$Psi$se, stations=i)
results.frame<-rbind(results.frame, results)
}
}
write.table(results.frame, "C:\\RWorkspace\\simulation_results.txt")
return(results.frame)
}
Yes,
rbindcan be slow; the faster thing is usually to make the matrix the right size to start with and fill it in appropriately. It’s also usually faster to fill in a matrix instead of a data frame.However, with the size you indicate, I would suspect that
markis what is slowing the function down and you won’t get much noticeable speedup by doing that. It would be easy to test that by storing a single result inrun.modeland then commenting that line out of your loop; that will tell you how much time it’s spending just storing the results. (You could also “profile” the function, but this would be simpler.)EDIT: I’m actually wrong; the size you indicate is big enough that the
rbindis quite possible causing problems. On my system, which is fairly fast and has a decent amount of memory, it takes 7.73 sec torbindusing data frames withn=20and only 0.09 sec withn=1, so clearly some memory churning is happening. As for speedup, withn=20it takes only 1.00 sec torbindmatrices and 0.033 sec to fill it in.