Rnotebook chunk execution extremely slow compared to console

Usually I don't notice a difference between the time of execution of a rnotebook chunk vs. console execution. But with my new company laptop, I'm noticing a difference within Rstudio but not vscode (using a .rmd file).

What could be causing the slower chunk execution in rnotebooks? I've noticed this delay across many experiments. It's very noticeable, whereas code is almost instant in vscode .rmd files

timein <- proc.time()
  x = 1:5000000,
  y = 1,
  z = x*0.5
timeout <- proc.time() - timein

Rstudio rnotebook timing:
user system elapsed
0.16 0.05 2.81
Rstudio Console timing:
user system elapsed
0.13 0.02 0.16
Vscode rmd chunk timing:
user system elapsed
0.16 0.01 0.22

Another example showing for loops:

Sample For Loop

timein <- proc.time() 
arr <- 0 
randnum <- rnorm(50000) 
for(i in 1:50000){
    arr[i] <- randnum[i]+1 } 
timeout <- proc.time() - timein 

Output differences

Rstudio - RNotebook

user  system elapsed  
0.01 0.01 2.52 

Vscode RMarkdown

user  system elapsed 
0.10 0.00 0.11 

You shouldn't use a loop. The function is already vectorised:

randnum <- rnorm(50000) 
arr <- randnum + 1

Hi Martin,

Thanks for the reply. I understand the function is vectorised, I was just using the for loop to demonstrate the inconsistencies between the platforms. I'm trying to understand why my Rnotebook is so much slower than Rmarkdown on other applications. This isn't an issue on my other computers, just this one. To further demonstrate, using your function below are the computation times between Rstudio Rnotebooks and VScode rmarkdown on my machine. Could the problem be my corporate network? :

Rstudio RNotebook:

   user  system elapsed 
   0.00    0.02    0.58

VSCode RMarkdown:

   user  system elapsed
   0.04    0.03    0.08

Ok, I misunderstood your intention.

Notebooks are meant to be interactive, so presumably that's why they take longer to execute.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.