我正在寻找对this question提出的问题最有效的解决方案:假设您有一个向量v
的字符串:
set.seed(314159)
library(stringi)
library(stringr)
v <- stringi::stri_rand_strings(10000, 4, pattern = "[A-Z]")
head(v)
#> [1] "FQGK" "YNQH" "IMNJ" "WUFU" "BBAR" "BZUH"
我想有效地返回一个逻辑,该逻辑表示给定的模式(例如"FOO"
)是否与v
中的任何字符串匹配。预期功能将像这样工作:
detect("FOO")
#> FALSE
detect("BAR")
#> TRUE
有几种方法可以使用基本的grep
函数或使用stringr::str_detect
来进行此操作,但是每种方法都涉及首先在v
的每个元素上匹配一个正则表达式,最多可以执行9,999个不必要的测试在我的例子中。找到单个匹配项后,有效的解决方案将停止评估。
对于每个解决方案detect.#
,我通过将其应用于所有三个字母组合c
进行基准测试:
c <- combn(LETTERS,3, FUN = function(x){paste(x, collapse = '')})
head(c)
#> [1] "ABC" "ABD" "ABE" "ABF" "ABG" "ABH"
我想出了几种可能的解决方案。首先,循环v
,以便在找到匹配项后不再进行不必要的模式匹配。如您所见,这是一个糟糕的主意,有很多开销:
detect.1 <- function(pattern){
for (i in 1:length(v)){
if (length(grep(pattern, v[i]))){return(TRUE)}
}
return(FALSE)
}
接下来,我们可以使用any()
和grepl()
或stringr::str_detect()
的组合,但是随后我们进行了不必要的匹配测试:
#str_detect() from stringr
detect.2 <- function(pattern){
any(str_detect(v, pattern) )
}
# any() and grepl()
detect.3 <- function(pattern){
any(grepl(pattern, v))
}
最后,如果我们知道一个字符从未出现在pattern
中,我们可以将v
折叠成一个由该字符分隔的单个字符串。然后一个grep
就足够了:
#collapse to long string
v_pasted <- paste(v, collapse = '_')
detect.4 <- function(pattern){
isTRUE(as.logical(grep(pattern, v_pasted)))
}
(已更新为使用bench::mark()
)
det1 <- expression(data.frame(c, "inV" = I(lapply(c, FUN = detect.1))))
det2 <- expression(data.frame(c, "inV" = I(lapply(c, FUN = detect.2))))
det3 <- expression(data.frame(c, "inV" = I(lapply(c, FUN = detect.3))))
det4 <- expression({
v_pasted <- paste(v, collapse = '_')
data.frame(c, "inV" = I(lapply(c, FUN = detect.4)))
})
bench::mark(
eval(det1),
eval(det2),
eval(det3),
eval(det4),
iterations = 5,
relative = TRUE
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
#> # A tibble: 4 x 10
#> expression min mean median max `itr/sec` mem_alloc n_gc n_itr
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 eval(det1) 76.9 77.0 76.8 77.2 1 1 Inf 1
#> 2 eval(det2) 4.02 4.03 4.04 4.05 19.1 735. Inf 1
#> 3 eval(det3) 2.77 2.79 2.79 2.80 27.6 735. Inf 1
#> 4 eval(det4) 1 1 1 1 77.0 1.22 NaN 1
grepl
比str_detect
快得多。粘贴方法是最快的,但是要求您具有一个分隔符,该分隔符不会出现在可能的搜索模式中。有没有我想念的更快的选择?
答案 0 :(得分:1)
stringi
软件包中的此功能应该更快:
any(stri_detect_fixed(v, pattern, max_count = 1))
长凳:
require(stringi)
detect.m <- function(pattern){
any(stri_detect_fixed(v, pattern, max_count = 1))
}
detm <- expression(data.frame(c, "inV" = I(lapply(c, FUN = detect.m))))
r <- bench::mark(
# eval(det1),
eval(det2),
eval(det3),
eval(det4),
eval(detm),
iterations = 5,
relative = TRUE
)
r[, 1:10]
# expression min mean median max `itr/sec` mem_alloc n_gc n_itr total_time
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 eval(det2) 4.83 5.39 5.02 5.94 1 600. 9 1 5.39
# 2 eval(det3) 3.85 3.69 3.80 3.31 1.46 600. 10 1 3.69
# 3 eval(det4) 1.35 1.32 1.36 1.20 4.08 1 1 1 1.32
# 4 eval(detm) 1 1 1 1 5.39 600. 9 1 1
# lets create larger test case for better comparison:
a <- expand.grid(lapply(1:5, function(x) LETTERS))
a <- do.call(paste0, a)
f10 <- a[10] # lets search for 10th element
last <- a[length(a)] # and last
length(a)
length(unique(a))
v <- a
detm <- function(pattern){
any(stri_detect_fixed(v, pattern, max_count = 1))
}
det4 <- function(pattern){
# should include paste
v_pasted <- paste(v, collapse = '_')
# isTRUE(as.logical(grep(pattern, v_pasted)))
isTRUE(grepl(pattern, v_pasted, fixed = T)) # faster
}
system.time(detm(last)) # 0.74
system.time(detm(f10)) # 0.33
system.time(det4(last)) # 3.38
system.time(det4(f10)) # 3.08