如何在R中初始化TRUE向量?

时间:2017-07-16 03:44:10

标签: r

假设我想初始化一个仅包含TRUE的长度为n的向量,其中n是一个正整数。

显然,我可以!logical(n)以及rep(TRUE,n)

但是,我想知道哪些更快,以及是否还有其他(更快)的选择。

1 个答案:

答案 0 :(得分:0)

我想知道内部函数 rep_len()rep.int() 是否可能更快。 事实并非如此。 rep() 是最简单的选项,与 rep_len() 一样快。

suppressMessages(require(microbenchmark))

n <- 1E6L
microbenchmark(
  rep(TRUE, n),
  rep_len(TRUE, n),
  rep.int(TRUE, n),
  !logical(n),
  !vector(length=n),
  times = 1E4L, units = "us"
)
#> Warning in microbenchmark(rep(TRUE, n), rep_len(TRUE, n), rep.int(TRUE, : Could
#> not measure a positive execution time for 1533 evaluations.
#> Unit: nanoseconds
#>                 expr     min      lq       mean  median      uq      max neval
#>         rep(TRUE, n) 1203400 1494900 1943086.70 1546600 1627950 25343300 10000
#>     rep_len(TRUE, n) 1224500 1497100 1978824.31 1549950 1630400 28793600 10000
#>     rep.int(TRUE, n) 1281700 1558100 2026079.65 1609650 1692350 26135600 10000
#>          !logical(n) 2184300 2726350 3659910.07 2876200 3329900 28890700 10000
#>  !vector(length = n) 2158900 2729200 3626122.66 2872750 3296150 28172100 10000
#>                units       0       0      76.12     100     100     1400 10000

logical()vector() 变慢的原因是显而易见的。他们需要 两个操作而不是一个:创建向量并将所有值更改为 TRUE 然后。因此,我们必须意识到 FALSE 值不存在速度优势:在这种情况下,logical()rep() 更快。

microbenchmark(
  logical(n),
  vector(length=n),
  times = 1E4L, units = "us"
)
#> Warning in microbenchmark(logical(n), vector(length = n), times = 10000L, :
#> Could not measure a positive execution time for 4059 evaluations.
#> Unit: nanoseconds
#>                expr    min     lq       mean median      uq      max neval
#>          logical(n) 461100 923400 1268676.20 985300 1110350 24575300 10000
#>  vector(length = n) 462400 923800 1268691.87 984850 1103850 25761700 10000
#>               units      0      0      19.45      0       0      900 10000

reprex package (v0.3.0) 于 2021 年 1 月 23 日创建