r-lib / vctrs

Generic programming with typed R vectors

Home Page:https://vctrs.r-lib.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Investigate `vec_match()` performance for integers

krlmlr opened this issue · comments

Slower than match() by 50%?

n <- 1E6
m <- 1E3
vs <- seq(n)
index <- seq(m)
value <- list(rev = as.list(rev(index)), nchar = as.list(nchar(index)))

bench::mark(
  "Rmatch" = {
    value_nw2 <- lapply(value, function(x) {
      unname(x[match(seq_along(vs), index)])
    })
  },
  "vec_match" = {
    value_nw2 <- lapply(value, function(x) {
      unname(x[vctrs::vec_match(seq_along(vs), index)])
    })
  }
)
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 Rmatch       13.6ms   14.1ms      69.7    34.6MB     279.
#> 2 vec_match    27.3ms   28.1ms      34.1    38.2MB     102.

Created on 2024-04-09 with reprex v2.1.0

Downstream: igraph/rigraph#1327 (comment)