by finnstats
How to Measure Execution Time in R, To compare the execution times of different expressions, use R’s microbenchmark package.
To do so, use the following syntax:
library(microbenchmark)
Let’s compare two different expressions’ execution times
microbenchmark(expression1, expression2))
The following example shows how to use this syntax in practice.
How To Become A Quantitative Analyst » finnstats
Example: Using microbenchmark() in R
Assume we have the following R data frame containing information about points scored by players on various basketball teams:
Make this example replicable.
set.seed(123)
Let’s create a data frame
df <- data.frame(team=rep(c('A', 'B'), each=500), points=rnorm(1000, mean=20))
Now we can view the data frame
Where can I find Data Science Internships » finnstats
head(df)
team points 1 A 19.43952 2 A 19.76982 3 A 21.55871 4 A 20.07051 5 A 20.12929 6 A 21.71506
Assume we want to compute the average points scored by players on each team using two different methods:
Method 1: Use aggregate() from Base R
https://finnstats.com/2023/01/21/how-to-measure-execution-time-in-r/

