I have a piece of code and total elapsed time is around 30 secs of which, the following code is around 27 secs. I narrowed the offending code to this:
d$dis300[i] <- h
So I change to this other piece and is now working really fast (as expected).
My question is why this is too slow against the second. The datos DF is around 7500x18 vars
First: (27 sec elapsed)
d$dis300 <- 0
for (i in 1:netot) {
h <- aaa[d$ent[i], d$dis[i]]
if (h == 0) writeLines(sprintf("ERROR. ent:%i dis:%i", d$ent[i], d$dis[i]))
d$dis300[i] <- h
}
Second: (0.2 sec elapsed)
d$dis300 <- 0
for (i in 1:netot) {
h <- aaa[d$ent[i], d$dis[i]]
if (h == 0) writeLines(sprintf("ERROR. ent:%i dis:%i", d$ent[i], d$dis[i]))
foo[i] <- h
}
d$foo <- foo
You can see both are the "same" but the offending one has this DF instead of a single vector.
Any comment is really appreciated. I came from another type of languages and this drove me nuts for a while. At least I have solution but I like to prevent this kind of issues in the future.
Thanks for your time,