予備的検討事項
、ケース・変更オペレーターの置換パターンで(実際には、Rで使用されるPCREライブラリの拡張です)。また、TRE正規表現パターンで.
を使用する場合は、改行にも一致し、PCREパターンでは.
に一致する改行があることを覚えておいてください。.
の前に(?s)
インラインDOTALL修飾子を使用する必要があります。 (または修飾子グループのような(?s:.*)
)。
RでTREとPCREの正規表現エンジンのパフォーマンスを比較する場合は、これらの2つのエンジンと文字通り同じテキストに一致する単純なパターンを使用する必要があります。 Windows 7の
ベンチマークテスト、LinuxのUbuntuの16.04、MacOSのシエラ10.12.6
私は主にWindowsでRを使用しますが、私は特にこのテストのためのLinux VM上でR 3.2.3をインストールしました。 MacOSの結果はt.kalinowski's answerから借りています。
のは、TRE(デフォルト)を比較してみましょうとマイクロベンチマークライブラリを使用してPCRE(perl=TRUE
)正規表現のパフォーマンス(benchmarking options in R以上を参照してください):テキストがWikipedia article about butterfliesある
library(microbenchmark)
。
WINDOWS
-------
Unit: microseconds
expr min lq mean median uq max neval
PCRE_1(txt) 163.607 165.418 168.65393 166.625 167.229 7314.588 5e+05
TRE_1(txt) 70.031 72.446 74.53842 73.050 74.257 38026.680 5e+05
MacOS
-----
Unit: microseconds
expr min lq mean median uq max neval
PCRE_1(txt) 31.693 32.857 37.00757 33.413 35.805 43810.177 5e+05
TRE_1(txt) 46.037 47.199 53.06407 47.807 51.981 7702.869 5e+05
Linux
------
Unit: microseconds
expr min lq mean median uq max neval
PCRE_1(txt) 10.557 11.555 13.78216 12.097 12.662 4301.178 5e+05
TRE_1(txt) 25.875 27.350 31.51925 27.805 28.737 17974.716 5e+05
TRE正規表現sub
:
# sub('.*\\((.*)\\).*', '\\1', txt)
# => [1] "formerly the superfamily \"Hedyloidea\""
PCRE_1 <- function(text) { return(sub('.*\\((.*)\\).*', '\\1', txt, perl=TRUE)) }
TRE_1 <- function(text) { return(sub('.*\\((.*)\\).*', '\\1', txt)) }
test <- microbenchmark(PCRE_1(txt), TRE_1(txt), times = 500000)
test
結果は以下の通りである。
txt <- "Butterflies are insects in the macrolepidopteran clade Rhopalocera from the order Lepidoptera, which also includes moths. Adult butterflies have large, often brightly coloured wings, and conspicuous, fluttering flight. The group comprises the large superfamily Papilionoidea, which contains at least one former group, the skippers (formerly the superfamily \"Hesperioidea\") and the most recent analyses suggest it also contains the moth-butterflies (formerly the superfamily \"Hedyloidea\"). Butterfly fossils date to the Paleocene, which was about 56 million years ago."
のがしようとするとsub
、Rでは非常に一般的sub
操作で括弧内の最後のテキストを抽出してみましょうWindowsの場合にのみが勝ち、2倍以上速くなります。 MacOSとLinuxの両方で、PCRE(perl=TRUE
)のバージョンは同様の比率で勝ちます。
それでは、重くや二重引用符内の単語を抽出バックトラッキングを使用していない正規表現のパフォーマンスを比較してみましょう:
# regmatches(txt, gregexpr("\"[A-Za-z]+\"", txt))
# => [1] "\"Hesperioidea\"" "\"Hedyloidea\""
PCRE_2 <- function(text) { return(regmatches(txt, gregexpr("\"[A-Za-z]+\"", txt, perl=TRUE))) }
TRE_2 <- function(text) { return(regmatches(txt, gregexpr("\"[A-Za-z]+\"", txt))) }
test <- microbenchmark(PCRE_2(txt), TRE_2(txt), times = 500000)
test
WINDOWS
-------
Unit: microseconds
expr min lq mean median uq max neval
PCRE_2(txt) 324.799 330.232 349.0281 332.646 336.269 124404.14 5e+05
TRE_2(txt) 187.755 191.981 204.7663 193.792 196.208 74554.94 5e+05
MacOS
-----
Unit: microseconds
expr min lq mean median uq max neval
PCRE_2(txt) 63.801 68.115 75.51773 69.164 71.219 47686.40 5e+05
TRE_2(txt) 63.825 67.849 75.20246 68.883 70.933 49691.92 5e+05
LINUX
-----
Unit: microseconds
expr min lq mean median uq max neval
PCRE_2(txt) 30.199 34.750 44.05169 36.151 43.403 38428.2 5e+05
TRE_2(txt) 37.752 41.854 52.58230 43.409 51.781 38915.7 5e+05
最高の平均値がMacOSの中で、LinuxではPCRE正規表現に属し、その違いはほとんど無視され、Windowsでは、TREがはるかに高速に動作します。
概要
TRE(デフォルト)正規表現ライブラリは、Windowsではるかに高速に動作することは明らかです。 のLinuxでは、PCRE正規表現はかなり高速です。 MacOSでは、バックトラックパターンでPCRE正規表現がそのOSのTREよりも速いので、PCRE正規表現は依然として好ましいです。
※ご希望の方は、ご利用ください。 –
これらの関数は正規表現を使用します。私は専門家ではありませんが、これらの正規表現のためのいくつかの異なる「味」のAKAエンジンがあることはわかっています。引数がする唯一のことは、perl風味を有効にすることです。 –
'perl = TRUE'は、パターンがPCRE(Perlではない)正規表現エンジンによって処理されるようにします。デフォルトのTRE正規表現エンジンで解析されたパターンとは異なるパターンが動作することに注意してください。 –