23 Measuring performance

23.1 Profiling

  1. Q: Profile the following function with torture = TRUE. What is surprising? Read the source code of rm() to figure out what’s going on.

    A: Unfortunately, the current version of profvis throws an error

23.2 Microbenchmarking

  1. Q: Instead of using bench::mark(), you could use the built-in function system.time(). But system.time() is much less precise, so you’ll need to repeat each operation many times with a loop, and then divide to find the average time of each operation, as in the code below.

    How do the estimates from system.time() compare to those from bench::mark()? Why are they different?

    A: As bench::mark() doesn’t calculate the mean value, we calculate it from the time list-column in the tibble output.

    Both approaches get the order of magnitude right. The results differ a little and bench::mark() is generally more precise.

  1. Q: Here are two other ways to compute the square root of a vector. Which do you think will be fastest? Which will be slowest? Use microbenchmarking to test your answers.

    A: Here, we’ll bench::mark the relative execution time of these expressions, with the fastest expression standardized to 1.

    We can see, that x ^ (1 / 2) takes about twice as long as exp(log(x)/2) to calculate the square root of x.