How to use list-argument in microbenchmark
Asked Answered
P

2

6

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.

Paladin answered 5/10, 2015 at 14:20 Comment(0)
E
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.

Entreaty answered 7/1, 2018 at 12:25 Comment(0)
P
5

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)))}) )
Paladin answered 5/10, 2015 at 14:20 Comment(2)
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)))).Absentminded
Thank you Konrad. I have edited my answer. They are some pretty interesting functions I wasn't aware of :)Paladin

© 2022 - 2024 — McMap. All rights reserved.