1

Consider the following example:

Test <- setRefClass(
    "Test", 
    fields = list(M = "matrix")
)

x <- lapply(1:20000, function(i) matrix(rnorm(100), nrow = 10, ncol = 10))
y <- lapply(x, function(M) Test(M = M))

xts <- Sys.time()
saveRDS(x, "x.rds")
xte <- Sys.time()

yts <- Sys.time()
saveRDS(y, "y.rds")
yte <- Sys.time()

print(xte - xts)
print(yte - yts)

The time it takes to save the "raw" list of matrices is about 0.48 secs vs 3.68 secs for the list of objects in my machine. I understand the objects might be more lengthy to save as they are more complex data structures but why is the difference so dramatic even when the objects have no more than the same matrices stored in them? On my real application I've been trying to save a list of about 17k objects which contain several fields and methods each but the whole list is only about 11 megabytes (by the output of object.size()). saveRDS() has been running for 30 minutes and the list has not yet been saved. Is saving collections of objects like this just a really bad idea or is there something that can be done for making it more efficient?

MikeKatz45
  • 545
  • 5
  • 16

0 Answers0