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.
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 are odd.
Therefore, we first need to initialize the vector we are going to use, populating it with a sequence of numbers from 1 to 1000:
vector <- seq(1:1000)