5

How do one use the list-argument in the microbenchmark function. I want to microbenchmark the same function with different inputs as in

microbenchmark(j1 = {sample(1e5)},
               j2 = {sample(2e5)},
               j3 = {sample(3e5)})

The following is not going to fly, as the list will just contain vectors and not unevaluated expressions.

microbenchmark(list = list(j1 = {sample(1e5)},
                          j2 = {sample(2e5)},
                          j3 = {sample(3e5)))

I would also want to generate the list using e.g. lapply.

Tobias Madsen
  • 857
  • 8
  • 10

2 Answers2

10

Just use alist:

microbenchmark(list = alist(a = Sys.sleep(0.005), b = Sys.sleep(0.01)))
#> Unit: milliseconds
#>  expr      min       lq      mean    median        uq       max neval cld
#>     a  5.02905  5.15946  5.447538  5.446029  5.612429  6.030764   100  a 
#>     b 10.04997 10.18264 10.431011 10.459569 10.547814 11.058911   100   b

alist handles its arguments as if they described function arguments. So the values are not evaluated, and tagged arguments with no value are allowed whereas list simply ignores them.

Hugh
  • 15,521
  • 12
  • 57
  • 100
4

We need to use the substitute or bquote function to get the unevaluated expressions in the list, e.g.

microbenchmark(list = list(j1 = bquote({sample(1e5)}),
                           j2 = bquote({sample(2e5)}),
                           j3 = bquote({sample(3e5)})))

The jobs can be generated using lapply, but we have to be careful with environments

jobs = lapply(1000*1:3, function(s) local({s = s; bquote(sample(.(s)))}) )
Tobias Madsen
  • 857
  • 8
  • 10
  • 3
    Rather than faffing around with `new.env()`, you can just put the statement inside a `local`: `local({s = s; bquote(sample(.(s)))})` (`bquote` and `substitute` both work but I generally prefer `bquote` for readability). In fact, this simple case even works without local environment (but that doesn’t generalise): `jobs = lapply(1000 * 1 : 3, function (s) bquote(sample(.(s))))`. – Konrad Rudolph Oct 05 '15 at 14:25
  • Thank you Konrad. I have edited my answer. They are some pretty interesting functions I wasn't aware of :) – Tobias Madsen Oct 06 '15 at 07:07