I'm working with vectors in R, storing bigz objects (GMP package). It works fine when the vector is not too big (up to 200 elems), but when the vector grows, the performance decrease dramatically.
Trying to get where the bottleneck is, I execute my programm with the profvis
library, and I get that the problem is the next piece of code:
res <- as.bigz(rep(0, length(resT))) #resT is a list of bigz of length 600
res[[1]] <- sum.bigz(a_bigz_elem, another_bigz_elem)
i <- 1
while (i <= length(resT) - 1) {
res[[i + 1]] <- sum.bigz(resT[[i + 1]], resE[[i + 1]])
i <- i + 1
}
and more specifically, in the 5th line (the elem assignment in the list).
I know this solution is better than a growing vector, i.e. res <- c(res, sum.bigz(resT[[i + 1]], resE[[i + 1]]))
, but I'm pretty sure there is a more efficient way to do the assignment.
I've read related post in SO and the Advanced R reference documentation, but nothing improves my performance. Any idea how can it be improved?
With
res <- as.bigz(rep(0, length(resT))) #resT is a list of bigz of length 600
you are not creating a list
. When you change it to:
res <- as.list(as.bigz(rep(0, length(resT))))
you create a list and the execution time should be reduced.
A reproducible example would look like:
library(gmp)
resT <- as.list(urand.bigz(600, seed=0))
resE <- as.list(urand.bigz(600, seed=1))
a_bigz_elem <- resT[[1]]
another_bigz_elem <- resE[[1]]
res <- as.list(as.bigz(rep(0, length(resT))))
res[[1]] <- sum.bigz(a_bigz_elem, another_bigz_elem)
i <- 1
while (i <= length(resT) - 1) {
res[[i + 1]] <- sum.bigz(resT[[i + 1]], resE[[i + 1]])
i <- i + 1
}