R Microbenchmark: How To Measure And Compare The Execution Of R Expressions
Want to share your content on python-bloggers? click here.
![](https://i0.wp.com/wordpress.appsilon.com/wp-content/uploads/2024/07/668d765a1e313eb16ab69fec_65ea0d821f7ca369883cb65c_6525256482c9e9a06c7a9d3c2F65e9e3e8b48aa724ae675184_41f9d760_R-1-p-800.webp?resize=578%2C325&ssl=1)
Are you still conducting runtime benchmarks in R through manual calls to `Sys.time()`? We don’t judge, but there’s a way more powerful and automated way to do the same – and more – with fewer lines of code.
Meet R Microbenchmark – a package that compares the execution of your R expressions by running them several times. You’ll typically want to use it to compare algorithms or functions that have the same goal, but different methodology of getting to it. Today you’ll learn all about this package through a couple of real-world examples. Let’s dive in!
Is R slow for processing huge volumes of data? We did the test on 1 billion rows.
Table of contents:
- R Microbench: How to Get Started
- R Microbenchmark: Testing on Real-World Data
- Summing up R Microbenchmark
R Microbenchmark: How To Get Started
The Microbenchmark package is available on CRAN, meaning you can install it easily through the R console:
Speaking of installation, here are all the packages you’ll need today. Install any that are missing:
We’ll keep things simple in this section, mainly just to showcase how R Microbenchmark works without worrying about the tasks at hand.
R has a `median()` function built in that computes the middle value of a vector. What we’ll do here is implement the function from scratch. This goes without saying, but you should never do this on your own – it’s just for demonstration purposes.
Then, we’ll create a vector of random integers and calculate the median with our custom function and the built-in function:
![](https://i0.wp.com/cdn.prod.website-files.com/654fd3ad88635290d9845b9e/667c10b311f6da3f52c3dc96_1.webp?w=578&ssl=1)
The values are identical, so let’s proceed.
The `microbench()` function allows you to declare multiple expressions. Basically, you’re assigning an expression result to a string name, so you can easily compare the results. The function will run the test for each expression 100 times by default (you’ll see how to change this later).
Here’s everything you need to do in code:
![](https://i0.wp.com/cdn.prod.website-files.com/654fd3ad88635290d9845b9e/667c10d3212353a27d70857c_2.webp?w=578&ssl=1)
On average, the built-in function is about 30 microseconds faster when compared to our custom implementation. Not an impressive difference, but surely lays the groundwork for more complex comparisons.
The R Microbenchmark package also allows you to display a violin plot of the benchmark results:
![](https://i0.wp.com/cdn.prod.website-files.com/654fd3ad88635290d9845b9e/667c10ef03063448a57a0fe4_3.webp?w=578&ssl=1)
The X-axis scale is tiny, but you can clearly see that the built-in function is faster. Let’s transfer this to a couple of real-world examples next.
R Microbenchmark: Testing On Real-World Data
You now know how R Microbenchmark works, so next, we’ll explore how it can help you in the real world. To follow along, download the NYC Yellow Taxi dataset for December 2023 (or any other month). The December dataset has almost 3.4 million records! Parquet files are fast to work with, so we’ve manually converted it to CSV.
The following two sections will compare two R options for working with CSV files – dplyr and data.table.
Reading Data
The first order of business is getting data from disk into R. The built-in `read.csv()` function will do the groundwork for dplyr, and `fread()` for datatable. It turns out dplyr isn’t really efficient when reading local CSV files, so we’ll reduce the number of time benchmark runs from 100 to 500:
![](https://i0.wp.com/cdn.prod.website-files.com/654fd3ad88635290d9845b9e/667c111c63975a81a356b704_4.webp?w=578&ssl=1)
Yes, you’re reading that right. Datatable is more than 10 times faster than dplyr on average when it comes to reading local CSV files.
A chart will paint that picture even better:
![](https://i0.wp.com/cdn.prod.website-files.com/654fd3ad88635290d9845b9e/667c1127dae915e1d3c3beea_5.webp?w=578&ssl=1)
If your data pipelines are running slow, check your data reading methodology. It might be a serious bottleneck.
Aggregating Data
Next, let’s discuss data aggregation. For demonstration purposes, let’s agree that the data is loaded into R through the following commands:
![](https://i0.wp.com/cdn.prod.website-files.com/654fd3ad88635290d9845b9e/667c114d38beaccd5c817fab_6.webp?w=578&ssl=1)
Onto the aggregation now. The goal is to calculate the number of, minimum, average, and maximum trip distances for a given month. The aggregation snippet includes calculating derived columns, filtering out records that don’t belong to the given month, grouping data, and calculating summary statistics.
Here are snippets for both dplyr and datatable aggregations:
![](https://i0.wp.com/cdn.prod.website-files.com/654fd3ad88635290d9845b9e/667c116c688efee43fd3f197_7.webp?w=578&ssl=1)
The dplyr code took forever to run, so we’ve reduced the number of iterations to 10. The results are clear – datatable is several orders of magnitude faster than dplyr in this case.
As before, you’re free to inspect the results visually:
![](https://i0.wp.com/cdn.prod.website-files.com/654fd3ad88635290d9845b9e/667c117a23d5d174995b3e5c_8.webp?w=578&ssl=1)
Speaking of visualization, the default styles may not be too appealing to some, so next, we’ll show what you can do about it.
Bonus: Autoplot Styling
Plot styling isn’t the main focus of this article, but we still wanted to briefly show how to tweak a couple of small things that will make a huge aesthetical difference in the end.
The result of the `autoplot()` function is a ggplot2 plot object, which means you can add layers to it just like you normally would.
The following snippet shows how to add a title, change its style, and how to change the overall theme:
![](https://i0.wp.com/cdn.prod.website-files.com/654fd3ad88635290d9845b9e/667c119a688efee43fd423d6_9.webp?w=578&ssl=1)
Nothing fancy, but you get the idea – you can threat the results of `autoplot()` just like any other ggplot2 visualization.
Summing Up R Microbenchmark
And that does it for today’s article. It was somewhat shorter than other pieces you’re accustomed to reading on the Appsilon Blog, but there isn’t much more to say about R Microbenchmark. It’s a simple package that does one thing and does it really well.
We see ease of use and simplicity as the main benefits of this package. You no longer need to call `Sys.time()` at appropriate places, nor do you need to run your experiments in loops. Nothing wrong with that approach either, but Microbench offers more for fewer lines of code. Win-win!
What are your thoughts on R Microbench? What’s your procedure for comparing runtimes in R? Let us know in our community.
Did you know ggplot2 can make animated charts? Read our guide on Race Charts with gganimate!
The post appeared first on appsilon.com/blog/.
Want to share your content on python-bloggers? click here.