Comparing an alternative function's performance using the microbenchmarking package

When dealing with efficiency issues, a fast way to evaluate two alternative functions can be really useful.

This recipe is going to show you how to do this quickly and effectively and display the results of your comparison in a ggplot diagram that is easy to understand.

Getting ready

This recipe is going to leverage the microbenchmark package to compute the function comparison and the ggplot2 package for comparison plotting:

install.packages(c("microbenchmark","ggplot2"))
library(microbenchmark)
library(ggplot2)

The example that follows is represented by two alternative functions to determine, for a given numeric vector, which elements of the vector are even and which ...

Get RStudio for R Statistical Computing Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.