sample(x, length) or x[runif(length, 1:(length(x)+1))] : what is the most rapid?

microbenchmark(
  sample(x = x, size=1000L, replace = TRUE)
, sample(x = x, size=1000, replace = TRUE)
, x[floor(runif(1000L, 1L, length(x)+1L))]
, x[floor(runif(1000, 1, length(x)+1))]
, x[runif(1000L, 1L, length(x)+1L)]
, x[runif(1000, 1, length(x)+1)]
, times = 10000L
)

Unit: microseconds
                                        expr    min      lq     mean  median      uq
 sample(x = x, size = 1000L, replace = TRUE) 42.202 46.0750 51.70049 50.4445 53.1295
  sample(x = x, size = 1000, replace = TRUE) 41.433 46.0935 51.52335 50.5570 53.0650
  x[floor(runif(1000L, 1L, length(x) + 1L))] 40.027 42.2450 49.22916 49.5545 52.0355
     x[floor(runif(1000, 1, length(x) + 1))] 40.112 42.1865 59.03723 49.5050 51.9370
         x[runif(1000L, 1L, length(x) + 1L)] 37.124 39.2570 46.15197 46.4555 48.9570
            x[runif(1000, 1, length(x) + 1)] 37.093 39.1935 54.89901 46.3630 48.7875

In conclusion, the most rapid is 
x[runif(1000, 1, length(x) + 1)]

But the difference is not very important.

Commentaires

Posts les plus consultés de ce blog

Standard error from Hessian Matrix... what can be done when problem occurs

stepAIC from package MASS with AICc

Install treemix in ubuntu 20.04