シリーズいろいろなものを測る:stri_duplicated

stringiパッケージのstri_duplicated関数は重複している文字列を真偽値で返してくれる関数である。
同じようなことは組み込み関数のduplicated関数でもできる。
そしてヘルプにはstri_duplicatedは遅いと書いてある。
測ってみよう。

library("dplyr")
library("microbenchmark")
df1 <- data_frame(V1=rep(LETTERS, 100000))
microbenchmark(
   base=df1[duplicated(df1$V1),],
   stri=df1[stringi::stri_duplicated(df1$V1),], times=5L)

結果はこちら。確かに遅い。

Unit: milliseconds
 expr      min       lq     mean   median       uq      max neval
 base 109.7088 121.3637 155.1631 124.9957 208.9821 210.7651     5
 stri 958.9795 959.8822 962.4511 960.1295 965.7052 967.5592     5

ヘルプの該当場所には以下のようにある。
duplicatedはロケール依存だがstri_duplicatedはそうではないとのこと。

Unlike duplicated and anyDuplicated, these functions test for canonical equivalence of strings (and not whether the strings are just bytewise equal) Such operations are locale-dependent. Hence, stri_duplicated and stri_duplicated_any are significantly slower (but much better suited for natural language processing) than their base R counterpart.

フムー