23.3 Microbenchmarking

Measurement of the performance of a very small piece of code is useful for comparing small snippets of code for specific tasks.

Credits: Google search-engine

Figure 23.2: Credits: Google search-engine

The {bench} package uses a high precision time.

bench::mark()
library(bench)
x <- runif(100)
(lb <- bench::mark(
  sqrt(x),
  x ^ 0.5
))
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 sqrt(x)    370.08ns 391.04ns  2375584.      848B     238.
#> 2 x^0.5        1.98µs   2.01µs   489264.      848B       0
  • heavily right-skewed distribution
require(ggbeeswarm)
#> Loading required package: ggbeeswarm
plot(lb)