Abbas
Abbas

Reputation: 897

Does preallocation of R list improve loop run time? how?

I am running a simulation in R, in which the outputs should be stored in numeric vectors in a variable of the type list. However, I am wondering why when I preallocated the list with numeric vectors, the computational time remains the same instead of reducing. My code is similar to the following hypothetical cases in which I have to use nested loops and store the results in the list. Here is the code for the case without preallocation:

n_times <- 5000

my_list <- list()

Sys.time()
start_time <- Sys.time()
for( i in 1:n_times){
  
  for (j in 1:10){
    df <- data.frame(y = rnorm(n = 200, mean = sample.int(10,1), sd = 4),
                     x1 = rnorm(n = 200, mean = sample.int(10,1), sd = 1),
                     x2 = rnorm(n = 200, mean = sample.int(10,1), sd = 4))
    model <- lm(y ~ x1 + x2, data = df)
    
    my_list[[as.character(j)]][i] <-  summary(model)$r.squared
  }
}

end_time <- Sys.time()
end_time - start_time

and here is the code for the case with preallocation:

# number of times the simulation to be run
n_times <- 5000
# preallocating the list of length 10 with numeric vectors of length n_times
my_list <- replicate(10, vector("numeric", n_times), simplify = F)
names(my_list) <- as.character(1:10)

Sys.time()
start_time <- Sys.time()
for( i in 1:n_times){
  
  for (j in 1:10){
    df <- data.frame(y = rnorm(n = 200, mean = sample.int(10,1), sd = 4),
                     x1 = rnorm(n = 200, mean = sample.int(10,1), sd = 1),
                     x2 = rnorm(n = 200, mean = sample.int(10,1), sd = 4))
    model <- lm(y ~ x1 + x2, data = df)
    
    my_list[[as.character(j)]][i] <-  summary(model)$r.squared
  }
}

end_time <- Sys.time()
end_time - start_time

Upvotes: 2

Views: 229

Answers (1)

Mohamed Desouky
Mohamed Desouky

Reputation: 4425

I think preallocating a list with just 5000 * 10 elements doesn't take much time , after profiling you code most time goes to lm and data.farme creations , see below

enter image description here

Upvotes: 1

Related Questions