Loops! Loops! Loops in R. A Microbenchmark

May 3, 2019
By

[This article was first published on R – Hi! I am Nagdev, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Loops are the holy grail in data science. You might use it when you want to repeat your task or a function or build a model say “n” times or iterations. There are quite few types of loops and most common ones are for and while. The main difference between while and for is, in while you run it until a condition is met like “run until you find an apple in a basket” whereas in for “you run until say n times”. Like I said earlier, for loops are a life saver. Interestingly in R, you have different types of loops similar to for.

  1. for: for is a regular for where you can run n times.
  2. foreach: for each is a parallel computing for loop where you have multiple iterations running in parallel. This is useful when you have computationally intensive loops such as training a model.
  3. apply: this is a special type of loop in R. This function can be applied to arrays, lists, matrices and data frames for both column and rows.

With various types of looping functions, its often difficult to decide which to use and when. I created a scenario with a dummy function and trying various loops at different data sizes. The micro benchmark results are as shown in below jupyter notebook (opens mybinder for interactive execution). It was intersting in the analysis to note that as the size of the data increases the time to execute with for and foreach increased. While for apply there was hardly any change.

Based on the following results, today I have started to embrace apply loops in R for many of my projects and have shown similar improvement in performance.

Let me know your comments below.

To leave a comment for the author, please follow the link and comment on their blog: R – Hi! I am Nagdev.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)