Execution Pause for X Seconds in R (Example)
This article shows how to force the R programming language to sleep for X seconds before continuing to execute.
Table of contents:
- Measuring Execution Time of Regular For-Loop
- Example: Force R to Wait 5 Seconds
- Video, Further Resources & Summary
Let’s jump right to the R syntax…
Measuring Execution Time of Regular For-Loop
For comparison, we’ll first run a regular for-loop without any sleeping time in between. We also measure the execution time of this for loop:
for(i in 1:10) { # Start for-loop start_time <- Sys.time() # Save starting time do_something <- runif(100000) # Do anything you want end_time <- Sys.time() # Save finishing time time_needed <- end_time - start_time # Calculate time difference print(paste("Step", i, "was finished after", time_needed, "seconds.")) # Print time difference } # [1] "Step 1 was finished after 0.00398898124694824 seconds." # [1] "Step 2 was finished after 0.00399613380432129 seconds." # [1] "Step 3 was finished after 0.00497984886169434 seconds." # [1] "Step 4 was finished after 0.00398898124694824 seconds." # [1] "Step 5 was finished after 0.00399208068847656 seconds." # [1] "Step 6 was finished after 0.00404691696166992 seconds." # [1] "Step 7 was finished after 0.00392913818359375 seconds." # [1] "Step 8 was finished after 0.00398993492126465 seconds." # [1] "Step 9 was finished after 0.00410890579223633 seconds." # [1] "Step 10 was finished after 0.00394511222839355 seconds." |
for(i in 1:10) { # Start for-loop start_time <- Sys.time() # Save starting time do_something <- runif(100000) # Do anything you want end_time <- Sys.time() # Save finishing time time_needed <- end_time - start_time # Calculate time difference print(paste("Step", i, "was finished after", time_needed, "seconds.")) # Print time difference } # [1] "Step 1 was finished after 0.00398898124694824 seconds." # [1] "Step 2 was finished after 0.00399613380432129 seconds." # [1] "Step 3 was finished after 0.00497984886169434 seconds." # [1] "Step 4 was finished after 0.00398898124694824 seconds." # [1] "Step 5 was finished after 0.00399208068847656 seconds." # [1] "Step 6 was finished after 0.00404691696166992 seconds." # [1] "Step 7 was finished after 0.00392913818359375 seconds." # [1] "Step 8 was finished after 0.00398993492126465 seconds." # [1] "Step 9 was finished after 0.00410890579223633 seconds." # [1] "Step 10 was finished after 0.00394511222839355 seconds."
As you can see based on the output of the RStudio console, the execution time of each run was close to zero. Now, let’s do the same with an execution pause in between…
Example: Force R to Wait 5 Seconds
If we want to force the R programming language to make a pause, we can use the Sys.sleep function. The following R code is exactly the same as before, but this time we are adding a break of 5 seconds to every run of the for-loop by using the Sys.sleep function:
for(i in 1:10) { # Start for-loop start_time <- Sys.time() # Save starting time do_something <- runif(100000) # Do anything you want Sys.sleep(5) # Wait 5 seconds end_time <- Sys.time() # Save finishing time time_needed <- end_time - start_time # Calculate time difference print(paste("Step", i, "was finished after", time_needed, "seconds.")) # Print time difference } # [1] "Step 1 was finished after 5.01288914680481 seconds." # [1] "Step 2 was finished after 5.01009678840637 seconds." # [1] "Step 3 was finished after 5.05642509460449 seconds." # [1] "Step 4 was finished after 5.00833010673523 seconds." # [1] "Step 5 was finished after 5.00653600692749 seconds." # [1] "Step 6 was finished after 5.00872683525085 seconds." # [1] "Step 7 was finished after 5.00770616531372 seconds." # [1] "Step 8 was finished after 5.00688481330872 seconds." # [1] "Step 9 was finished after 5.01025819778442 seconds." # [1] "Step 10 was finished after 5.00811791419983 seconds." |
for(i in 1:10) { # Start for-loop start_time <- Sys.time() # Save starting time do_something <- runif(100000) # Do anything you want Sys.sleep(5) # Wait 5 seconds end_time <- Sys.time() # Save finishing time time_needed <- end_time - start_time # Calculate time difference print(paste("Step", i, "was finished after", time_needed, "seconds.")) # Print time difference } # [1] "Step 1 was finished after 5.01288914680481 seconds." # [1] "Step 2 was finished after 5.01009678840637 seconds." # [1] "Step 3 was finished after 5.05642509460449 seconds." # [1] "Step 4 was finished after 5.00833010673523 seconds." # [1] "Step 5 was finished after 5.00653600692749 seconds." # [1] "Step 6 was finished after 5.00872683525085 seconds." # [1] "Step 7 was finished after 5.00770616531372 seconds." # [1] "Step 8 was finished after 5.00688481330872 seconds." # [1] "Step 9 was finished after 5.01025819778442 seconds." # [1] "Step 10 was finished after 5.00811791419983 seconds."
The RStudio console is showing the result: Each run took about 5 seconds longer, i.e. there was a sleeping pause within each round of the loop.
Video, Further Resources & Summary
Do you need further info on the R syntax of this tutorial? Then you might have a look at the following video of the Statistics Globe YouTube channel. I explain the content of this article in the video.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, I can recommend having a look at some of the related articles on this website:
- Introduction to System Calls & Commands
- Measuring Execution Time of Function in R
- R Functions List (+ Examples)
- The R Programming Language
In this article, I explained how to suspend the execution of an R code for a certain time interval in the R programming language. Tell me about it in the comments section, if you have any further questions.
Statistics Globe Newsletter