Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use list-argument in microbenchmark

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.

like image 567
Tobias Madsen Avatar asked Oct 05 '15 14:10

Tobias Madsen


2 Answers

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.

like image 157
Hugh Avatar answered Nov 15 '22 06:11

Hugh


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)))}) )
like image 25
Tobias Madsen Avatar answered Nov 15 '22 05:11

Tobias Madsen